Default Db Value Override

When creating new records with Yii, Yii is putting the DB default values for those fields into the form fields.

How can I override this?

One method would be to use the afterFind() method of CActiverecord to replace the default database values

Hi,

Yes I tried that but it does not replace them, at least it does not on create anyway.

James.

Try overriding afterConstruct().

The constructor is where the defaults are set:




public function __construct($scenario='insert') 

{ 

    if($scenario===null) // internally used by populateRecord() and model() 

        return; 


    $this->setScenario($scenario); 

    $this->setIsNewRecord(true); 

    $this->_attributes=$this->getMetaData()->attributeDefaults;


    $this->init(); 


    $this->attachBehaviors($this->behaviors()); 

    $this->afterConstruct(); 

}



just insert in your model class

public $field1 = ‘value1’;

public $field2 = ‘value2’;

public $field3 = ‘value3’;

It works in my project :)