How to automate whodidit behaviour and timestamps in yii ActiveRecord model?

To automate whodidit and timestamps in yii activerecord model using beforesave() method. If the rule validation got success then only the beforesave() method will invoke. Without satisifying rule() in activerecord model, this method will not work.

[php]
protected function beforeSave() {
if(parent::beforeSave()) {
if($this->isNewRecord) {
$this->created_on = time();
$this->created_by = (int)Yii::app()->user->id;
}
$this->updated_on = time();
$this->updated_by = (int)Yii::app()->user->id;
return true;
} else return false;
}
[/php]

Permanent link to this article: https://blog.openshell.in/2013/03/how-to-automate-whodidit-behaviour-and-timestamps-in-yii-activerecord-model/

Leave a Reply

Your email address will not be published.