afterSave() TimeOut Error

I’m attempting to use afterSave() in a model, but php throws a memory limit error with the following code:




  public function afterSave()

  {

      $model = $this->findByPk($this->id);

      $model->notes = 'test';

      $model->save();


      return parent::afterSave();

  }



Is this the wrong way to save the current model using afterSave()?

I suppose beforeSave() is more appropriate.

Maybe something like:




  public function beforeSave()

  {

      if(parent::beforeSave())

           $this->notes = 'test';

      else

           return false;

  }

judjing from the manual (not tested).

HTH

You actually recursivly call the afterSave() method each time you call save() - That causes the memory limit error.

You don’t need to modify afterSave() since save() does save the record in the database (unless there was an error).

Thank you both for the replies.

I did originally use beforeSave().

In my case I want to run several conditions based on the current records relations:




public function beforeSave()

{

  if($this->customer->type == '1') {

    $this->type= '1';

  }

}



Of course, the code above doesn’t work because $this is not referencing a real DB record and the AD relations fail. Hence, the attempted use of afterSave() to load the ‘real’ record and try to use AD relations for my conditions.

I’ll try to rethink my conditions in order to use beforeSave()