Derp
Derp

Reputation: 109

Pass variable from beforeDelete to afterDelete() in CakePHP

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()?

Upvotes: 2

Views: 2217

Answers (1)

Guerra
Guerra

Reputation: 2790

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']);       
}

Upvotes: 11

Related Questions