Pass variable from beforeDelete to afterDelete() in CakePHP

cakephp, php

Solution

You can simply declare this var outside method scope.

private  $info;
function beforeDelete() {
$this->info = $this->find('first', array(
    'conditions' => array('Media.id' => $this->id),
));
}

function afterDelete() {
   unlink(WWW_ROOT . 'img/photos/' . $this->info ['News']['related_id'] . "/" . $this->info ['Media']    ['file']);       
}

Problem

I want to delete a file after a record in database has been deleted. Because I use some data from a related model to build a path to image I need to fetch this data in the beforeDelete(). ``` function beforeDelete() { $info = $this->find('first', array( 'conditions' => array('Media.id' => $this->id), )); } function afterDelete() { unlink(WWW_ROOT . 'img/photos/' . $info['News']['related_id'] . "/" . $info['Media']['file']); } ``` How to properly access `$info` array in `afterDelete()`?

Original source