CakePHP 3 - array_merge():参数 #2 不是数组

CakePHP 3 - array_merge(): Argument #2 is not an array

我正在开发的 CakePHP 3 应用程序中有一个奇怪的错误,我无法弄清楚问题是什么 is.The 应用程序和问题如下:

  1. 我有一个心愿单
  2. 那个愿望清单可以有很多愿望清单项目
  3. 我正在使用 Angular/Ajax 拨打电话并删除 WishlistItem

    http://domain.com/wishlist_items/delete/219
    
  4. 这不起作用,它 returns 出现以下错误:

    array_merge(): Argument #2 is not an array 
    
    /­vendor/­cakephp/­cakephp/­src/­ORM/­Association/­DependentDeleteTrait.php 54
    
  5. 问题中的WishlistItemsController.php是:

    public function delete($id = null) {
        $this->autoRender = false;
    
        $this->request->allowMethod(['post', 'delete']);
        $wishlistItem = $this->WishlistItems->get($id);
    
        if ($this->WishlistItems->delete($wishlistItem)) {
            echo json_encode(['message' => ['type' => 'success', 'text' => 'This wishlist item has been deleted successfully']]);
        }
        else {
            echo json_encode(['message' => ['type' => 'error', 'text' => 'This wishlist item could not be deleted']]);
        }
        return;
     }
    
  6. 愿望清单项目没有依赖项,错误来自此文件http://api.cakephp.org/3.0/source-class-Cake.ORM.Association.DependentDeleteTrait.html#19-57第 54 行

  7. 我的 table 文件如下所示:

    WishlistsTable.php
    
    $this->hasMany('WishlistItems', [
        'foreignKey' => 'wishlist_id',
        'dependent' => true,
        'cascadeCallbacks' => true,
    ]);
    
    ==================================================
    
    WishlistItemsTable.php
    
    $this->addBehavior('Timestamp');
    $this->addBehavior('Ratings.Ratable');
    $this->belongsTo('Wishlists', [
        'foreignKey' => 'wishlist_id'
    ]);
    
    $this->addBehavior('CounterCache', [
        'Wishlists' => ['items_count']
    ]);
    

我不明白为什么在 Table 没有任何依赖项时使用 cascadeDelete 函数。

堆栈:

Whoops\Exception\ErrorException thrown with message "array_merge(): Argument #2 is not an array"

Stacktrace:
#15 Whoops\Exception\ErrorException in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php:54
#14 Whoops\Run:handleError in /var/www/domain.com/dev/website/vendor/gourmet/whoops/src/Error/WhoopsHandler.php:27
#13 Gourmet\Whoops\Error\WhoopsHandler:_displayError in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php:139
#12 Cake\Error\BaseErrorHandler:handleError in <#unknown>:0
#11 array_merge in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php:54
#10 Cake\ORM\Association\HasMany:cascadeDelete in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/AssociationCollection.php:273
#9 Cake\ORM\AssociationCollection:cascadeDelete in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Table.php:1705
#8 Cake\ORM\Table:_processDelete in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Table.php:1643
#7 Cake\ORM\Table:Cake\ORM\{closure} in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Database/Connection.php:557
#6 Cake\Database\Connection:transactional in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Table.php:1648
#5 Cake\ORM\Table:delete in /var/www/domain.com/dev/website/src/Controller/WishlistItemsController.php:139
#4 App\Controller\WishlistItemsController:delete in <#unknown>:0
#3 call_user_func_array in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Controller/Controller.php:429
#2 Cake\Controller\Controller:invokeAction in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Routing/Dispatcher.php:114
#1 Cake\Routing\Dispatcher:_invoke in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Routing/Dispatcher.php:87
#0 Cake\Routing\Dispatcher:dispatch in /var/www/domain.com/dev/website/webroot/index.php:37

如果在您调用 delete() 的 table 对象上真的只有 BelongsTo 关联,那么这样的控制流应该永远不会发生,因此需要更多的关联从别处定义。

鉴于 Ratings.Ratable 行为属于 dereuromark/cakephp-ratings,我怀疑问题出在为 HasMany 行为添加到 table 的关联:

https://github.com/dereuromark/cakephp-ratings/.../Behavior/RatableBehavior.php#L85

$this->_table->hasMany('Ratings', [
        'className' => $this->_config['rateClass'],
        'foreignKey' => $this->_config['foreignKey'],
        'unique' => true,

        'conditions' => '', // <<<<<< There it is

        'fields' => '',
        'dependent' => true,
        //'table' => 'sandbox_ratings'
    ]
);

该值最终作为 DependentDeleteTrait::cascadeDelete() 中的第二个参数传递给 array_merge(),因此出现错误。

所以,这是插件中的错误,我建议您报告它。作为快速解决方法,您可以覆盖 table class 中的条件,例如

$this->association('Ratings')->conditions([]);