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.

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;

Permanent link to this article:

Leave a Reply

Your email address will not be published.