As in the book, I want to extend CActiveRecord so that when an object has columns such as "creation_stamp", "modification_stamp", "create_user_id" etc they are automatically updated.
But I want this class to be usable even for objects that don’t include all of those columns, so that an object can only have cdate/mdate or cuser/muser or neither.
Something like
protected function beforeValidate() {
if ($this->isNewRecord) {
//NEW record
//cstamp
if (property_exists($this, 'cstamp'))
$this->cstamp = NOW;
//mstamp
if (property_exists($this, 'mstamp'))
$this->mstamp = NOW;
} else {
//OLD record
//mstamp
if (property_exists($this, 'mstamp'))
$this->mstamp = NOW;
}
return parent::beforeValidate();
}
Using property_exists() does not work of course since columns are "magically" accessible properties, and isset() is not enough since it will return false even when a property exists but is null.
Is there a way to check if a column property exists?
Would array_key_exists(‘col’, $obj->attributes) be sufficient?