I am new to yii.I have a table "sponsor" which has id,name,address as columns.For that table i wrote the model class which [b]extends "CModel"[/b]
Class Sponsor extends CModel
{
public function attributeNames()
{
return array('id'=>'Id','name'=>'Name','address'=>'Address');
}
public function getbyid($id)
{
$connection=yii::app()->db;
$dataReader=$connection->createCommand("CALL sp_getbyid_sponsor($id)")->query();
$rows=$dataReader->readAll();
return ($rows);
}
}
I have the following code in the CGridview.
'buttons'=>array
(
'view' => array
(
'label'=>'View',
'imageUrl'=>Yii::app()->request->baseUrl.'/images/icon_tool_view.png',
'url'=>'yii::app()->createUrl("admin/view",array("id"=>$data["id"]))',
'options'=>array('class'=>'view')
),
My controller is:
public function actionView($id)
{
$model=new Sponsor;
$rawdata=$model->getbyid($id);
$this->render('view',array('model'=>$model));
}
I want to display the name and address of the sponsor in the textbox using the CActiveForm when i click the view button in CGridview.
But when i run the code it shows the [b]CException:Property "Sponsor.name" is not defined error.[/b]
is the attributeNames() function which i wrote in Model class right?or is there any other function is needed for Model Class.If anybody knows pls reply to this one.
Using this class you don’t need to write something like “getbyid($id)” as you already have the method Sponsor::model()->findByPk(1) which will return a model object matching the row with primary key 1
Your attributeNames function is not correct. It should return a list with the attribute names. For the labels there is a separate function attributeLabels().
Because you are planning to inherit from CModel, make sure to check its class reference:
You just create an empty Sponsor object and pass it to the view. The $rawData variable isn’t used. And do not overwrite attributeNames(). Define public properties as attributeNames() will return them automatically.
class Sponsor extends CModel
{
public $name;
public $adress;
}
and then do something like
public function actionView($id)
{
//do the DAO stuff here and then
$model->attributes=$DAOResultArrayWithAttributes;
$this->render('view',array('model'=>$model));
Class Sponsor extends CModel
{
public $name;
public $address;
public $id;
public function attributeNames()
{
return array( 'id', 'name', 'address' );
}
public function attributeLabels()
{
return array('id'=>'Id','name'=>'Name','address'=>'Address');
}
}
But when i run this code.It just display the white page.NO error.No result.