attributeNames() in CModel

Hi all,

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);


      }





} 

My view is:

view.php:

         <?php $form=$this->beginWidget('CActiveForm'); ?>


         <?php echo $form->labelEx($model,'name'); ?>


         <?php echo $form->textField($model,'name',array('size'=>45,'maxlength'=>45)); ?>


         <?php echo $form->labelEx($model,'address'); ?>


         <?php echo $form->textField($model,'address',array('size'=>45,'maxlength'=>45)); ?>


         <?php echo CHtml::submitButton('Submit'); ?>


     <?php $this->endWidget(); ?>

in sponsor.php:

         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.

thanks in advance.

I think you are reinventing the wheel here as long as it isn’t vital to use DAO instead of ActiveRecord in your case.

If you have a table than you shouldn’t use CModel but CActiveRecord which has all the methods you need to do the CRUD stuff. Take a look at this

http://www.yiiframework.com/doc/guide/1.1/en/database.ar

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

greetings,

Hannes

Thanks for your reply.

I know about the CActiveRecords and their use.But my requirement is DAO usage.I want to get the same result using DAO.

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:

http://www.yiiframew.../api/1.1/CModel

Your attributeNames should look something like


return array( 'id', 'name', 'address' );

and you can simply rename your current implemenation of attributeNames to attributeLabels, as it will do the trick just fine.

PS also make sure you have defined the properties id, name and address

In your code you say:




$model=new Sponsor;

$rawdata=$model->getbyid($id);

$this->render('view',array('model'=>$model));



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));

}

Thanks for your reply genn.

I tried that as follows.




      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.

Ok, my bad. It looks like the magic methods __get and __set should also be implemented.

It works like this:


class TestModel extends CModel

{

	protected $id;

	protected $name;

	protected $address;

	

	public function attributeNames()

	{

		return array( 'id', 'name', 'address' );

	}

	

	public function attributeLabels()

	{

		return array( 'id' => 'ID label', 'name' => 'Name label', 'address' => 'Address label' );

	}

	

	public function __get( $name )

	{

		if( property_exists( $this, $name ) )

		{

			return $this->$name;

		}

		else

		{

			return parent::__get( $name );

		}

	}

	

	public function __set( $name, $value )

	{

		if( property_exists( $this, $name ) )

		{

			$this->$name = $value;

		}

		else

		{

			parent::__set( $name, $value );

		}

	}

}

Its important to make your attributes private or protected, so they cannot be accessed from the outside.

You can use the validators ans set different scenarios as usual.