How to add more property into model

Hello guys,

When I use gii tool to general a model, it default general all fields to model’s property, right?

So, when I wanna add a new field into my table, how can I add this property into model.

I’d tried:




class myModel extends CActiveRecord{

     public $newProperty;


     public function _get(){

         return $this->newProperty;

     }


     public function _set($newProperty){

         $this->newProperty = $newProperty;

     }


}



Is there somethins wrong with my codes, when I try to display the new property in gridview:




....

array(

  'header' => 'New Property',

  'type' => 'raw',

  'value' => $date->newProperty

)



Nothing display with my code :D

any suggestion :D?

Thanks

use just public $newProperty… without the _get() and _set()

But that is just for "additional" attributes ie those that are not part of the database…

If you just add a new field to the table… you don’t need to declare a variable… just set some validation rules… and use the new field as $model-><fieldname>

Hi, thanks for reply Mdomba.

I’d tried you way, if I add $newProperty without _get() and _set(), it displays nothing!

Well, if I set validation rules, it bring me error: "Property model_name.newProperty is not defined."

I’d added into rules() and attributeLabels(), but it seem not work :D





class myModel extends CActiveRecord{

     private $_newProperty;


     public function getNewProperty(){

         return $this->_newProperty;

     }


     public function setNewProperty($newProperty){

         $this->_newProperty = $newProperty;

     }


}


// to use it

$model->newProperty = 'myvalue';


echo $model->newProperty;



Check the magic __set and __get methods of CComponent.

Thank you so much for reply! :lol:

I’d fixed this problem. As Mdomba said, I just need to add some validation rules for the $newProperty -> it work :D

And I found the same topic here.

Thanks again Mdomba, Antonio Ramirez

@Antonio Ramirez: I’ll try your suggest, may be that’s another way to this problem, thanks again. :D

Cheers

My approach is more of an extension, widget concept… mdomba suggestion is more model realistic… congrats man

Hey guys,

I’ve been fighting with the same issue for the past couple of hours and finally found the solution that worked for me.

So I just wanted to share that you would need to flush your cache


Yii::app()->cache->flush()

if you have enabled the caching of the schema via the ‘schemaCachingDuration’ in your database connection configuration.

Cheers