模型回调beforeDelete

编程入门 行业动态 更新时间:2024-10-25 13:25:55
本文介绍了模型回调beforeDelete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试删除图像时删除这些图像的容器与级联模型::删除

I'm trying to delete images when deleting the container of those images with a cascading model::delete

级联工作正常,但我不能得到

The cascading works fine, but I can't get the model call back afterDelete to work properly so I can delete the actual image files when doing the delete.

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语句至少执行我将添加进一步验证unlink。

The if statement fails each time, even though I know there are images in the table. If I can get the if statement to at least execute i will add further validation on the unlink.

推荐答案

这样:

在beforeDelete获取图像数据

in beforeDelete get the images data

function beforeDelete(){ $relatedImages = $this->RelatedImage->find('all', array('conditions' => array('RelatedImage.container_id' => 'containerId'))); $this->relatedImages = $relatedImages; $this->currentId = $this->id; //I am not sure if this is necessary return true; }

然后在afterDelete()as Oscar建议做实际删除的图像:

then in the afterDelete() as Oscar suggest do the actual delete of the image:

function afterDelete(){ $relatedImages = $this->relatedImages; $containerId = $this->currentId; //probably this could be just $this->id; 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); } }

这样即使保存模型,

HTH

更多推荐

模型回调beforeDelete

本文发布于:2023-08-01 09:15:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   模型   beforeDelete

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!