I addedd the protected function beforeSave to a model class in order to fill some fields that are not user input.
The Controller is calling the ‘save’ method but beforeSave is not being called. I even tried to put an exception as the first line of the beforeSave function but nothing happens.
public function beforeSave() {
if ($this->isNewRecord)
$this->created = new CDbExpression('NOW()');
else
$this->modified = new CDbExpression('NOW()');
return parent::beforeSave();
}
Note that the function is declared as public and there is a call to the parent’s beforeSave() method also
This is working around here, have you tried this way?
I always declare all after/before functions as "protected", because they are protected in the parent class and should not be called outside it (or any descendant).
I don’t understand why people always say to return true in the end of “before” methods. If you’ll return true, an event “onBefore…” won’t be called! You should always put “return parent::beforeSave()” in the end of redefined beforeSave() method (same for other “before” methods, and without “return” for “after” methods).
And I don’t think it’s the problem there, since man said he put Exception at the first line…
If validation doen’t pass you would not reach the (protected) beforeSave()method. BTW remember to call parent or the onBeforeSave event would not fire.
There is also a (public) beforeSave() event handler, e.g in CActiveRecordBehavior.