FriendsOfCake / cakephp-upload

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

Cake PHP 5 - Object of class Laminas\Diactoros\UploadedFile could not be converted to string #609

Open stosef opened 5 hours ago

stosef commented 5 hours ago

At first let me thank all authors of this plugin for their work!

I use cakephp-upload in a CakePHP 3 project and it works great.

Now I want to use it with CakePHP 5 and I can't get it to work.

Here is the config for the table:

class CustomersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array<string, mixed> $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('customers');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->addBehavior('Josegonzalez/Upload.Upload', [
            'customer_logo' => [
                'path' => 'Files/logos/'
            ],
        ]);
    }
}

This is my config/plugins.php:

<?php

return [
    'DebugKit' => [
        'onlyDebug' => true,
    ],
    'Bake' => [
        'onlyCli' => true,
        'optional' => true,
    ],
    'Migrations' => [
        'onlyCli' => true,
    ],
    'Josegonzalez/Upload' => [],
];

This is how I use it in my template:

<?php
echo $this->Form->create($customer, [
    'type' => 'file'
]);
?>
<?= $this->Form->control('customer_logo', ['type' => 'file']); ?>

When I try to upload a file I get "Object of class Laminas\Diactoros\UploadedFile could not be converted to string"

Bildschirmfoto vom 2024-09-24 12-33-10

Debugger shows that the value that gets written to the DB was not cast to a string. I think it should be a string at this point?

Bildschirmfoto vom 2024-09-24 12-36-44

Does anyone have a working example with CakePHP 5? Am I missing some config or code?

Thank you

stosef commented 2 hours ago

I guess it has something to do with a breaking change in CakePHP 5 regarding file uploads: https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#http

ADmad commented 1 hour ago

The plugin has been compatible with CakePHP 5.x since quite a while. Use one of CakePHP's help forum to figure out your issue.