by prettyscripts on 2010-11-09 12:39:00
update: thanks to mindochin and josh who left a comment below, please check this post for the correct solution.
by default if date is not entered in a form, the value is saved as '0000-00-00'. yii does not automatically save it as null. to save the field as null, it needs to be done manually.
update (or add) beforeSave() function in model file:
PHP:
protected function beforeSave() | |
{ | |
if ($this->name_of_date_field == '') | |
$this->setAttribute('name_of_date_field', null); | |
| |
return parent::beforeSave(); | |
} |
the following code is not specific to any attribute types. if no value is entered and null allowed, then save the value as null.
PHP:
protected function beforeSave() | |
{ | |
foreach ($this->getTableSchema()->columns as $column) { | |
if ($column->allowNull == 1 && $this->getAttribute($column->name) == '') | |
$this->setAttribute($column->name, null); | |
} | |
| |
return parent::beforeSave(); | |
} |