Skip to content

Instantly share code, notes, and snippets.

@davialexandre
Created January 5, 2012 14:51
Show Gist options
  • Save davialexandre/1565566 to your computer and use it in GitHub Desktop.
Save davialexandre/1565566 to your computer and use it in GitHub Desktop.
A simple behavior to store the original loaded activerecord values and use it to check if any was changed
<?php
class AttributesBackupBehavior extends CActiveRecordBehavior {
private $old_attributes;
public function afterFind($event) {
parent::afterFind($event);
foreach($this->owner->attributes as $name => $value) {
$this->old_attributes[$name] = $value;
}
}
public function attributesChanged() {
$changed_attributes = array_diff($this->owner->attributes, $this->old_attributes);
return !empty($changed_attributes);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment