yii: save empty field as null

by prettyscripts on 2012-01-18 10:52:00

phpyii

this is an updated post in reference to a previous post about saving a field to null when no value is entered. thanks to those who left comment, this is the correct (and easier!) way to do it.

add a new rule to the model:

PHP:

protected funcion rules() {
    return array(
    ....
        array('name_of_data_field''default''setOnEmpty' => true),
    ....
    );
}

now the field will be saved as null if data is not entered.

Tags: code, null, php, save, yii

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)