1. CakePHP: Deleting and the afterDelete() callback

    August 4, 2008 by andyg

    Here is the problem I just had. I was trying to delete an image off the disk after all references to it in the database had gone - kinda like unlink(). I couldn’t work out how to access the data in the model. I need to know the filename, so I can delete it. It seems that the only way to do it is:

    1. Define a var to hold the filename

      private $_tmpImageName;
    2. Populate this in the beforeDelete() callback

      function beforeDelete() 
      {
          $this->_tmpImageName = $this->read(null, $this->id);
          return true;
      }
    3. Then access it in the afterDelete() callback, with something like this:

      function afterDelete()
      {
          unlink($this->_tmpImageName['Image']['name']);
          return true;
      }

    If you know of a better way, let me know.