Skip to content

Instantly share code, notes, and snippets.

@crisu83
Last active December 14, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/5047438 to your computer and use it in GitHub Desktop.
Save crisu83/5047438 to your computer and use it in GitHub Desktop.
<?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