1. CakePHP: MySQL “BETWEEN” with find parameters

    December 10, 2008 by andyg

    I use the BETWEEN in SQL queries quite often so when I couldn’t work out how to use it in CakePHP’s $model->find() method, I was more than a little frustrated. The normal Google search didn’t help, but I’ve managed to work it out with a quick look at the core CakePHP code.

    It’s a simple as:

    $fp = array( 'conditions' => array( 'id BETWEEN ? AND ?' => array( 286, 291 ) ) );
    $this->Goal->find('all', $fp);

  2. 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.