RESOLVED: Josegonzalez/Upload.Upload: Invalid data type, must be an array or \ArrayAccess instance

I’m trying to use the Josegonzalez/Upload.Upload plugin with CakePHP 3.x to manage file uploads.

My file field in the database is named data_file. Accordingly, I’ve set the appropriate field configuration in the Model.

    $this->setTable( 'jobs' );
    $this->setPrimaryKey( 'id' );

    $this->addBehavior( 'Timestamp' );
    $this->addBehavior( 'Utils.WhoDidIt',
                        [
                            'fields' => [ 'id', 'username' ],
                        ] );

    $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' ] );
        },
    ] );

In upload.ctp, I have the correct field name:

<?= $this->Form->create( $job, [
    'type' => 'file'
] ); ?>

<?= $this->Form->input( 'data_file', [
    'label' => __( 'Upload CSV file' ),
    'type'  => 'file',
    'class' => 'form-control'
] ); ?>

Here’s the 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}

What am I doing wrong?

Thanks.

your 'nameCallback' should be inside 'data_file' block

1 Like

Wow! How did I miss that… :smiley: ??

Thank you very much, Graziel. That fixed it.