milesj / uploader

[Deprecated] A CakePHP plugin for file uploading and validating.
MIT License
193 stars 73 forks source link

Deleting Files through a has many association #163

Closed ghost closed 10 years ago

ghost commented 10 years ago

I setup a hasmany association between products and images. I defined a AttachmentBehavior in the image model. When adding a product the images are uploaded and saved fine. When I delete the record the image record and main image is only removed but not the 2 additional transforms I defined. I can't figure out why only 1 of the 3 images are only removed.

public $actsAs = array(
        'Uploader.Attachment' => array(
                'image' => array(
                    'dbColumn' => 'image_orig',
                    'nameCallback' => 'transformName',
                    'uploadDir' => 'img/product_images/',
                    'finalPath' => 'img/product_images/',
                    'overwrite' => true,                    
                    'transforms' => array(
                                    'max' => array(
                                        'dbColumn' => 'image_max',
                                        'nameCallback' => 'formatName',
                                        'uploadDir' => 'img/product_images/max/',
                                        'finalPath' => 'img/product_images/max/',
                                        'class' => 'resize',
                                        'width' => 800,
                                        'height' => 600,
                                        'aspect' => false
                                    ),
                                    'thumb' => array(
                                        'dbColumn' => 'image_thumb',
                                        'nameCallback' => 'formatName',
                                        'uploadDir' => 'img/product_images/thumb/',
                                        'finalPath' => 'img/product_images/thumb/',
                                        'class' => 'resize',
                                        'width' => 100,
                                        'height' => 100,
                                        'aspect' => false
                        )           )
                )

            )
        );
milesj commented 10 years ago

Are the child records deleted but not the images? Or are the child records still there also?

ghost commented 10 years ago

This is my image table. only the first image gets deleted when deleting the product record associated.

screen shot 2013-12-30 at 3 45 23 pm

ghost commented 10 years ago

What did you mean by child records, My form in products has one image input. When submitting it is creating 1 record in images any the behavior saves 3 version of the image. When deleting the product record the record in images deletes and the first image but not the 2 additional transforms.

milesj commented 10 years ago

Yeah that's what I meant.

That all looks good. What versions are you using?

ghost commented 10 years ago

cakephp 2.4.3, uploader ver 4.5.0, php 5.5.3. I did email some model/controller clips yesterday for you to look at.

ghost commented 10 years ago

What direction should I research?

milesj commented 10 years ago

Won't have time to look at this until after the holidays, but you can debug a bit. Basically just see if the beforeDelete() is being called for images.

https://github.com/milesj/uploader/blob/master/Model/Behavior/AttachmentBehavior.php#L259

If it's not, it may be a limitation of Cake. If it is being called, make sure that all the fields with files are deleted here.

https://github.com/milesj/uploader/blob/master/Model/Behavior/AttachmentBehavior.php#L443

ghost commented 10 years ago

Did a little debug in attachmentBehavior.php line 470

pr($columns[$column]) ;pr($value);

data returned was good, so we are still holding the data to be deleted.

Array
(
    [image_orig] => image
    [image_max] => image
    [image_thumb] => image
)
image
img/product_images/04f6b3ea183eb9d70981161c4424a6cf525afd9f.png
image
img/product_images/max/04f6b3ea183eb9d70981161c4424a6cf525afd9f.png
image
img/product_images/thumb/04f6b3ea183eb9d70981161c4424a6cf525afd9f.png

the next line is

 if ($this->_deleteFile($model, $columns[$column], $value)) {
$save[$column] = '';
ghost commented 10 years ago

After further testing as long as you don't separate the images into folders all 3 images get deleted correctly. can we add this function?

milesj commented 10 years ago

Figured it out.

You also need to be using absolute URLs for uploadDir.

http://milesj.me/code/cakephp/uploader#faq-3

ghost commented 10 years ago

I went ahead and set the following.

define('UPLOAD_DIR', WWW_ROOT . '/img/product_images/max/');

Then in the model for the transforms

'transforms' => array(
                    'imageMax' => array(
                        'class' => 'resize',
                        'nameCallback' => 'formatName',
                        'append' => '-max',
                        'prepend' => '',
                        'uploadDir' => UPLOAD_DIR,
//                        'finalPath' => 'product_images/',
                        'dbColumn' => 'image_max',
                        'defaultPath' => '',
                        'overwrite' => true,
                        'self' => false,
                        'width' => PRODUCT_MAX_WIDTH,
                        'height' => PRODUCT_MAX_HEIGHT,
                        'aspect' => false

All 3 upload fine. When deleting the record this or still remains.

milesj commented 10 years ago

Have you updated versions? I tested it last night with your exact configuration and it worked.

ghost commented 10 years ago

Will test again, didn't see you updated to 4.5.1