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

时间:2015-12-01 20:25:16

标签: cakephp model associations cakephp-3.0 cascade

我正在开发的CakePHP 3应用程序中有一个奇怪的错误,我无法确定问题所在。应用程序和问题如下:

  1. 我有一份愿望清单
  2. 该愿望清单可以有许多愿望清单项目
  3. 我正在使用Angular / Ajax进行调用并删除WishlistItem

    http://domain.com/wishlist_items/delete/219
    
  4. 这不起作用,它会返回以下错误:

    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. 我的表格文件如下所示:

    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']
    ]);
    
  8. 当这个表没有任何依赖项时,我无法理解为什么使用cascadeDelete函数。

    STACKSTRACE:

    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
    

1 个答案:

答案 0 :(得分:1)

如果您正在调用BelongsTo的表对象上确实只有delete()个关联,那么这样的控制流应该永远不会发生,因此需要从其他地方定义更多的关联。

鉴于Ratings.Ratable行为属于 dereuromark/cakephp-ratings ,我怀疑问题是为HasMany定义的条件行为添加到表中的关联:

<强> 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'
    ]
);

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

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

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