我試圖刪除與級聯模式這些圖像的容器時刪除圖像::刪除型號回調beforeDelete
級聯工作正常,但我不能讓模型回調afterDelete正常工作所以我可以在刪除時刪除實際的圖像文件。
function beforeDelete() {
$containerId = $this->id;
$numberOfImages = $this->RelatedImage->find('count', array('conditions' => array('RelatedImage.container_id' => 'containerId')));
if ($numberOfImages > 0){
$relatedImages = $this->RelatedImage->find('all', array('conditions' => array('RelatedImage.container_id' => 'containerId')));
foreach ($relatedImages as $image) {
$myFile = WWW_ROOT . 'image' . $containerId . '_i' . $image['RelatedImage']['id'] . '.jpg';
unlink($myFile);
$myThumb = WWW_ROOT . 'img/' . $image['RelatedImage']['thumbnail'];
unlink($myThumb);
}
return true;
} else{
return false;
}
}
if語句每次都失敗,即使我知道表中有圖像。如果我能得到if語句,至少可以執行,我會在取消鏈接上添加更多驗證。