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:
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>
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.