FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

Thumbnail shell not working #432

Closed golusin closed 7 years ago

golusin commented 7 years ago

I'm trying to regenerate thumbnails for all the Product records. However, shell script doesn't do anything, apparently due to model naming problem.

I'm using CakePHP 2.8.6 and here are two models related to this issue.

Attachment

<?php
class Attachment extends AppModel {
  public $actsAs = array(
    'Upload.Upload' => array(
      'attachment' => array(
        'thumbnailMethod' => 'php',
        'thumbnailSizes' => array(
          'full' => '1024w',
          'large' => '800w',
          'medium' => '180x120',
          'thumb' => '100x100'
        ),
      )
    ),
  );

  public $belongsTo = array(
    'Product' => array(
      'className' => 'Product',
      'foreignKey' => 'foreign_key',
    )
  );
}
?>

and Product

<?php
App::uses('AppModel', 'Model');

class Product extends AppModel {

  public $belongsTo = array(
    'User',
    'Category'
  );

  public $hasMany = array(
    'Image' => array(
      'className' => 'Attachment',
      'foreignKey' => 'foreign_key',
      'conditions' => array(
        'Image.model' => 'Product',
      ),
      'dependent' => true
    )
  );
?>

When I try to run shell script and put Image as model name, I get Model 'Image' not found. error message. However, when I put Attachment as model name, script fails to find the files. All the files are uploaded to app/webroot/files/image/attachment/*. When I output $sourceFilePath variable in ThumbnailShell.php shell script I can see that the script is looking for them at app/webroot/files/attachment/attachment/*.

Can anybody help me to resolve this one :(

davidyell commented 7 years ago

Sounds like a model alias issue to me. Although the file upload path doesn't seem logical to me either. I would have expected webroot/files/attachment/<field>/<id>/<files>

golusin commented 7 years ago

Yes, the path is exactly what you are expecting... <field> in Attachment model where the filename is stored is named attachment.

I don't understand why shell script can't work with Image instead of Attachment when everything is working perfectly find when uploading files... all the records are created and files uploaded to app/webroot/files/image/attachment/<id>/

I'm not sure how to resolve this one :( Any ideas?

davidyell commented 7 years ago

Probably just needs a PR to use an alias() call on the Model.

Although it does use loadModel() so it should be taking care of aliases already.

golusin commented 7 years ago

It fails on L43 and L48. And if I comment out these two lines then I can see that the paths are wrong.

davidyell commented 7 years ago

Then it seems clear that your model class cannot be loaded and doesn't have the behaviour attached. Still seems like an issue with the class aliases to me.

I've not tried it with aliases as I don't tend to use them, sorry.

josegonzalez commented 7 years ago

loadModel won't load the model from an association alias. You'll need to make a change to add alias support. I'll gladly accept a pull request, but as CakePHP 2.x is not the current stable version, I won't be fixing this in mainline on my own.