You need to store the old attributes in a local property in the AR class so that you can compare the current attributes with these old ones at any time.
Step 1 Add a new property to the AR class:
// Stores old attributes on afterFind() so we can compare // against them before/after save protected $oldAttributes;
Step 2 Override Yii afterFind() and save the original attributes immediately after receiving them.
public function afterFind(){ $this->oldAttributes = $this->attributes; return parent::afterFind(); }
Step 3 Compare the old and new attributes in beforeSave/afterSave or anywhere else that you like inside the AR class. In the example below, we check to see if the property named "level" has changed.
public function beforeSave() { if(isset($this->oldAttributes['level']) && $this->level != $this->oldAttributes['level']){
source share