我正在开发的CakePHP 3应用程序中有一个奇怪的错误,我无法确定问题所在。应用程序和问题如下:
我正在使用Angular / Ajax进行调用并删除WishlistItem
http://domain.com/wishlist_items/delete/219
这不起作用,它会返回以下错误:
array_merge(): Argument #2 is not an array
/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php 54
有问题的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;
}
愿望清单项目没有家属,错误来自此文件http://api.cakephp.org/3.0/source-class-Cake.ORM.Association.DependentDeleteTrait.html#19-57
第54行
我的表格文件如下所示:
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']
]);
当这个表没有任何依赖项时,我无法理解为什么使用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
答案 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([]);