FriendsOfCake / cakephp-upload

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

Polymorphic model error #424

Closed Greyg-POPEI closed 7 years ago

Greyg-POPEI commented 7 years ago

Hello,

I try to use a polymorphic attachment model like in 2.x (http://cakephp-upload.readthedocs.io/en/2.x/polymorphic.html) in 3.x CakePHP project.

I have the same configuration: Table documents (just like your 'attachments' table). Table posts (just like your 'posts' table).

When I try to upload my file i get this error : Cannot insert the value NULL into column 'name', table 'documents'; column does not allow nulls. INSERT fails.

Request data look like this :

[id] => 110
[documents] => Array
        (
            [0] => Array
                (
                    [name] => file.pdf
                    [type] => application/pdf
                    [tmp_name] => C:\Windows\Temp\php893C.tmp
                    [error] => 0
                    [size] => 1433418
                    [model] => Posts
                    [foreign_key] => 110
                )
        )

My models look like this :

class DocumentsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->table('documents');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->addBehavior('Josegonzalez/Upload.Upload', [
            'name' => [
                'fields' => [
                    // if these fields or their defaults exist
                    // the values will be set.
                    'dir' => 'dir',
                    'size' => 'filesize', // defaults to `size`
                    'type' => 'mimetype', // defaults to `type`
                ],
        ]]);
    }
}
class PostsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('posts');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Documents', [
            'foreignKey' => 'foreign_key',
            'conditions' => array('Documents.model' => 'Posts'),
        ]);
    }
}

I also look in UploadBehavior.php beforeSave function.

            if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) {
                if (Hash::get($settings, 'restoreValueOnFailure', true)) {
                    $entity->set($field, $entity->getOriginal($field));
                    $entity->dirty($field, false);
                }
                continue;
            }

I don't have any error during upload, but the loop break here.

Do you have any ideas what i'm doing wrong ? Any help will be appreciated :)

Thanks

josegonzalez commented 7 years ago

What does your form field look like?

Greyg-POPEI commented 7 years ago

My form use this Angular-JS plugin : ng-file-upload : https://github.com/danialfarid/ng-file-upload

Thank for you answer

Greyg-POPEI commented 7 years ago

A little up

Thank you

gmmx commented 7 years ago

Same over here. Did you figure out this issue?

loopingGIT commented 7 years ago

I actually copy most of the code from @Greyg-POPEI (Table structure and relations) and it worked. This is how the form looks like:

//In my case, notes hasMany Attachments. I am attaching the behavior to the field 'file'
//Template/Notes/add.ctp
<?= $this->Form->create($note, ['type' => 'file']) ?>
    <fieldset>
        <legend><?= __('Add Note') ?></legend>
        <?php
            echo $this->Form->input('title');
            echo $this->Form->input('text');
            //attachments
            echo $this->Form->input('attachments.0.file', ['type' => 'file']);
            echo $this->Form->input('attachments.0.model', ['value' => 'Notes']);
            echo $this->Form->input('attachments.1.file', ['type' => 'file']);
            echo $this->Form->input('attachments.1.model', ['value' => 'Notes']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
gmmx commented 7 years ago

cheers!

jorisvaesen commented 7 years ago

Seems like it's no issue related to the plugin itselfs.