我想删除数据库中的记录后删除文件.因为我使用相关模型中的一些数据来构建图像路径,所以我需要在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']);
}
Run Code Online (Sandbox Code Playgroud)
如何正确访问$info
数组afterDelete()
?
Gue*_*rra 11
您可以简单地在方法范围外声明此var.
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']);
}
Run Code Online (Sandbox Code Playgroud)