On Fri, Jul 16, 2010 at 7:35 AM, Warren <[email protected]> wrote:
> 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;
> }
> }
>
> The if statement fails each time, even though I know there are images
> in the table.
My situation is a bit different than yours but this is what I have in
my Gallery model:
protected $_tmp_slug;
function beforeDelete()
{
$this->_tmp_slug = $this->field('slug');
return true;
}
function afterDelete()
{
$data = $this->Image->find(
'all',
array(
'conditions' => array(
'Image.gallery_id' => $this->id
),
'contain' => array(
'Preview',
'Thumbnail'
)
)
);
foreach($data as $d)
{
if ($this->Image->Thumbnail->delete($d['Thumbnail']['id']))
{
unlink(WWW_ROOT . $d['Thumbnail']['directory'] . '/' .
$d['Thumbnail']['basename']);
}
if ($this->Image->Preview->delete($d['Preview']['id']))
{
unlink(WWW_ROOT . $d['Preview']['directory'] . '/' .
$d['Preview']['basename']);
}
if ($this->Image->delete($d['Image']['id']))
{
unlink(WWW_ROOT . $d['Image']['directory'] . '/' .
$d['Image']['basename']);
}
}
$gallery_thumb_id = $this->GalleryThumb->field(
'id',
array('GalleryThumb.gallery_id' => $this->id)
);
$this->GalleryThumb->delete($gallery_thumb_id);
rmdir(WWW_ROOT . 'img/galleries/' . $this->_tmp_slug);
$this->_tmp_slug = null;
}
GalleryThumb is a simple model to specify which Thumb will be used to
represent an entire Gallery.
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en