afterFind() Not called after $model->save() ?

Hello,

I have been using Yii for a few months now, but this is the first time I have had to use it in this way. I have dynamic forms and then yet more dynamic forms dependent on the results of the previous forms. Those dynamic forms can nest any number of fields in the forms. So I chose to serialize the arrays.

To make things easy I did




	public function beforeSave(){

	   parent::beforeSave();

	   if( is_array( $this->commodity_subtype ) ) // throw the array into the DB.

	      $this->commodity_subtype = serialize( $this->commodity_subtype );

	   else if ( !$this->commodity_subtype ) // doesn't exist

	      $this->commodity_subtype = serialize( array() );

	   else // single value

	      $this->commodity_subtype = serialize( array( $this->commodity_subtype ) );

	   return true;

	}



AND




   protected function afterFind(){

      parent::afterFind();

      $this->commodity_subtype = $this->commodity_subtype ? unserialize( $this->commodity_subtype ) : array();

      return true;

   }



Now, when I submit the form I basically serialize the data into the database. That works. When I go to /form/view/### it works as expected. What does not work as expected is that when I re render the same page – I get an error caused by the still serialized data. Now, let’s say that Save() doesn’t reload the model for an update… I tried to do

refresh() and it did not run through afterFind()… thoughts ?

The afterFind() method will not be called on a refresh() call. It seems like a call to $this->afterFind() following the refresh() call should work.

(not tested)

/Tommy

Options and its behaviors do not have a method or closure named "afterFind".

This makes sense – it’s a protected method. Hm. Still hacking away at this issue. Redirecting works for obvious reasons, but I need to display errors. That’s the issue here. Reloading the submitted form to display errors. I suppose I could abstract the logic to a public method to be called on demand. I think I’ll try that.

You should be able to override refresh() in your model




public function refresh()

{

  parent::refresh();

  $this->afterFind();

}



(untested)

/Tommy