afterDelete() get model data: $this->caption

Problem is that i can’t think how to get old data from deleted row :) if its possible

In model file




  protected function afterDelete() {

	Yii::log('caption = '.$this->caption, 'info', 'dummy afterDelete'); // this i see in my log file. so i know it gets there <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />

  }



the same code on afterSave work fine, ofcourse (look below)




  protected function afterSave() {

	Yii::log('caption = '.$this->caption, 'info', 'dummy afterSave');

  }



and to not cretae new post, another question. How can i get old data in afterSave




  protected function afterSave() {

	Yii::log('old caption = '.$this->_caption, 'info', 'dummy afterSave');

	Yii::log('updatedor new caption = '.$this->caption, 'info', 'dummy afterSave');

  }



http://www.yiiframework.com/forum/index.php?/topic/11294-solvedis-it-possible-to-get-old-value-in-beforesave/page__view__findpost__p__55447__fromsearch__1

thanks, but… can it be done without new query on database…?

I’m not sure, but i think there was an behavior extension for this. Basic principle: Store the original attribute values inside a custom [b]afterFind/b method (e.g. in another property private $_oldValues;). Then you can access the old values from this property in [b]afterSave/b.

sounds good. i will try it, thanks ;)

with afterSave it worked fine, afterDelete not ;/

will try some other workarounds

As Mike metioned I’m sure there is a behavior for this but can’t find it.

Where is $this->_caption being set?

This is the simplest example I can think, also read this http://www.yiiframework.com/wiki/9/ its not quite what you want but might help.





  private $_oldValues = array();

  

  public function afterFind()

  {

    parent::afterFind();

    $this->_oldValues = $this->attributes;

  }

  

  public function afterDelete()

  {

    parent::afterDelete();

    $this->_oldValues['caption']; //field value you want to access

  }



Just a note:

If you use a delete call that affects multiple records (like Some::model()->deleteAll(‘id < 5’) ) afterDelete() will not be called for these records! This is by design because this command issues a DELETE SQL statement to the DB without reading in all the records before. You should be aware of this minor limitation of afterDelete().

Not exactly what’s you are searching for, but a good example of creating a behavior: http://www.yiiframework.com/wiki/9/how-to-log-changes-of-activerecords/

this is exactly what i did ;)