CakePHP-FileUpload-Plugin icon indicating copy to clipboard operation
CakePHP-FileUpload-Plugin copied to clipboard

Model does not update unless image is also uploaded

Open martinbean opened this issue 13 years ago • 4 comments

I’ve a model that I’ve added the file upload plugin to as a behavior. My model is set up as follows:

<?php
class Promoter extends AppModel {

    public $name = 'Promoter';
    public $actsAs = array(
        'Containable',
        'FileUpload.FileUpload' => array(
            'allowedTypes' => array(
                'gif' => array(
                    'image/gif'
                ),
                'jpg' => array(
                    'image/jpeg',
                    'image/pjpeg'
                ),
                'png' => array(
                    'image/png'
                )
            ),
            'fields' => array(
                'name' => 'image_filename',
                'type' => 'image_type',
                'size' => 'image_size'
            ),
            'fileNameFunction' => 'sanitizeFileName',
            'required' => false
        )
    );

    public function sanitizeFileName($filename) {
        $imagesDir = 'promoters';
        if (!is_dir($imagesDir)) {
            mkdir($imagesDir);
            chmod($imagesDir, 0755);
        }
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        return $imagesDir . DS . $this->data[$this->alias]['id'] . '.' . $extension;
    }
}

As you can see, required is set to false. The Promoter model is saved successfully if I also include an image for upload (the values are set in the database row, and the actual image is uploaded to the file system); however, if I don’t select an image to upload, then the database record is not updated despite getting a flash message saying record has been updated.

Any one able to see what the problem is? Have I configured something incorrectly, or is this just because this is the in-development branch and it’s not been addressed yet?

martinbean avatar Nov 11 '12 22:11 martinbean

I have the same problem, though only allowing PDFs. Were you able to find a solution?

CodingZen avatar Nov 14 '12 21:11 CodingZen

Reading the file_upload_settings.php file, it seems the key required is what determines whether a file upload’s needed to save the record:

/**
  * Behavior Setting Only.
  *
  * required determines and checks if a file was sent to the server.
  * @var boolean
  * - if true: a file is required to be uploaded to save relative records
  * - if false: related records will saved even if there is no uploaded file (default)
  */
'required' => false

But setting this to false in my model still doesn’t save the record upon submission.

I’ll have a dig around and see if I can find out what’s going on under the hood. I’ll also be tempted to start my own plugin since the CakePHP 2.0 branch of this one seems dead, and version 3 of CakePHP is actively being developed.

martinbean avatar Nov 15 '12 10:11 martinbean

Unfortunately I've stopped updating this plugin and refactored it into a Helper+Behavior in my new Icing plugin. I may come back to it one day and fix it for 2.0 but I changed so much in the backend and dropped the Component feature all together it wouldn't be a smooth upgrade.

Feel free to use the Icing version of this: https://github.com/AudiologyHoldings/Icing

webtechnick avatar Nov 15 '12 12:11 webtechnick

Does the Icing version fix the above problem?

Edit:

Nevermind. The patch here fixed the problem. https://github.com/sdoney/CakePHP-FileUpload-Plugin/commit/693a9b7d20f0ce4fac921ee3939e547cdf18baa9

CodingZen avatar Nov 15 '12 16:11 CodingZen