FriendsOfCake / cakephp-upload

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

Error: Invalid data type #467

Closed matibaski closed 7 years ago

matibaski commented 7 years ago

I'm receiving following error:

Invalid data type, must be an array or \ArrayAccess instance.

Stack trace:
#0 ~/vendor/josegonzalez/cakephp-upload/src/Model/Behavior/UploadBehavior.php(84): Cake\Utility\Hash::get(0, 'restoreValueOnF...', true)

My setup looks like this:

// src/Model/Table/UsersTable.php
$this->addBehavior('Josegonzalez/Upload.Upload', [
    'image_file',
]);

// src/Template/Users/edit.ctp
<?= $this->Form->create($user, ['type' => 'file']) ?> // form tag
echo $this->Form->input('image_file', ['class' => 'form-control', 'type'=>'file']); // form field

// patch & save in UsersController.php
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {

I didn't do nearly anything at all, just tried to get it running out of the box.

My table:

CREATE TABLE `users` (
  `id` bigint(11) NOT NULL,
  `role_id` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'user',
  `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(61) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `first_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `image_file` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `passkey` varchar(40) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `timeout` timestamp NULL DEFAULT NULL,
  `published` tinyint(1) NOT NULL DEFAULT '0',
  `disabled` tinyint(1) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL,
  `created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8_unicode_ci;
davidyell commented 7 years ago

Looks to me like you've failed to set an array of configuration for your image_file field. This means that instead of your field being the array key, it's instead the value.

[0 => 'image_file'];
// vs
['image_file' => $myConfigArray]

Take a look at the example in the docs.

sourjya commented 7 years ago

Hi, I'm also getting the exact same error and I've set the filed config correctly...

In JobsTable.php ` $this->setTable( 'jobs' ); $this->setPrimaryKey( 'id' ); $this->addBehavior( 'Timestamp' );

    $this->addBehavior( 'Josegonzalez/Upload.Upload', [
        // You can configure as many upload fields as possible,
        // where the pattern is `field` => `config`
        //
        // Keep in mind that while this plugin does not have any limits in terms of
        // number of files uploaded per request, you should keep this down in order
        // to decrease the ability of your users to block other requests.
        'data_file'         => [
            'fields' => [
                'dir' => 'file_dir'
            ]
        ],
        'nameCallback' => function ( $data, $settings ) {
            return strtolower( $data[ 'name' ] );
        },
    ] );

`

Stack trace: ` 2017-08-24 05:15:48 Error: [InvalidArgumentException] Invalid data type, must be an array or \ArrayAccess instance. Request URL: /admin/jobs/bulk-upload Referer URL: http://myproject.localhost/admin/jobs/bulk-upload Stack Trace:

0 D:\xampp\htdocs\myproject\vendor\josegonzalez\cakephp-upload\src\Model\Behavior\UploadBehavior.php(84): Cake\Utility\Hash::get(Object(Closure), 'restoreValueOnF...', true)

1 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Event\EventManager.php(414): Josegonzalez\Upload\Model\Behavior\UploadBehavior->beforeSave(Object(Cake\Event\Event), Object(App\Model\Entity\Job), Object(ArrayObject))

2 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Event\EventManager.php(391): Cake\Event\EventManager->_callListener(Array, Object(Cake\Event\Event))

3 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Event\EventDispatcherTrait.php(78): Cake\Event\EventManager->dispatch(Object(Cake\Event\Event))

4 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\ORM\Table.php(1789): Cake\ORM\Table->dispatchEvent('Model.beforeSav...', Array)

5 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\ORM\Table.php(1723): Cake\ORM\Table->_processSave(Object(App\Model\Entity\Job), Object(ArrayObject))

6 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\ORM\Table.php(1446): Cake\ORM\Table->Cake\ORM{closure}()

7 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Database\Connection.php(668): Cake\ORM\Table->Cake\ORM{closure}(Object(Cake\Database\Connection))

8 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\ORM\Table.php(1447): Cake\Database\Connection->transactional(Object(Closure))

9 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\ORM\Table.php(1724): Cake\ORM\Table->_executeTransaction(Object(Closure), true)

10 D:\xampp\htdocs\myproject\src\Controller\Admin\JobsController.php(73): Cake\ORM\Table->save(Object(App\Model\Entity\Job))

11 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Controller\Controller.php(440): App\Controller\Admin\JobsController->bulkUpload()

12 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(119): Cake\Controller\Controller->invokeAction()

13 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(93): Cake\Http\ActionDispatcher->_invoke(Object(App\Controller\Admin\JobsController))

14 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\BaseApplication.php(78): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

15 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))

16 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(59): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

17 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))

18 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

19 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))

20 D:\xampp\htdocs\myproject\vendor\markstory\asset_compress\src\Middleware\AssetCompressMiddleware.php(56): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

21 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): AssetCompress\Middleware\AssetCompressMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))

22 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(92): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

23 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))

24 D:\xampp\htdocs\myproject\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php(52): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

25 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): DebugKit\Middleware\DebugKitMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))

26 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

27 D:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Server.php(80): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))

28 D:\xampp\htdocs\myproject\webroot\index.php(37): Cake\Http\Server->run()

29 {main}

`

jorisvaesen commented 7 years ago

@sourjya You've defined nameCallback as a field and not as a option within a field. Please have a look in the documentation.