Last active
December 14, 2015 06:59
-
-
Save crisu83/5047438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @return array the behavior configurations (behavior name=>behavior configuration) | |
*/ | |
public function behaviors() { | |
return array( | |
'audit' => array('class' => 'common.extensions.audit.behaviors.AuditBehavior'), | |
'image' => array('class' => 'vendor.crisu83.yii-image.behaviors.ImageBehavior'), | |
); | |
} | |
/* --- */ | |
class AuditBehavior extends CActiveRecordBehavior { | |
/** | |
* @var string the name of the id column. | |
*/ | |
public $idAttribute = 'id'; | |
private $_oldAttributes; | |
protected function afterFind($event) { | |
$this->_oldAttributes = $this->owner->getAttributes(); | |
} | |
protected function beforeSave($event) { | |
$owner = $this->getOwner(); | |
foreach ($owner->getAttributes() as $name => $value) { | |
if ((string)$value !== (string)$this->_oldAttributes[$name]) { | |
/* @var AuditChanger $user */ | |
$user = Yii::app()->getUser(); | |
$entry = new AuditEntry; | |
$entry->changer = $user->modelClass; | |
$entry->changeId = $user->{$user->idAttribute}; | |
$entry->model = get_class($owner); | |
$entry->modelId = $owner->{$this->idAttribute}; | |
$entry->attribute = $name; | |
$entry->oldValue = $this->_oldAttributes[$name]; | |
$entry->newValue = $this->owner->attributes[$name]; | |
$entry->save(false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment