I have an odd error in a CakePHP 3 Application I am developing and I can't figure what the issue is.The application and issue is as follows:
I am using Angular/Ajax to make a call and delete a WishlistItem
http://domain.com/wishlist_items/delete/219
This does not work and it returns the following error:
array_merge(): Argument #2 is not an array
/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php 54
The WishlistItemsController.php
in question is:
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;
}
The Wishlist Items have no dependents and the error is coming from this file http://api.cakephp.org/3.0/source-class-Cake.ORM.Association.DependentDeleteTrait.html#19-57
line 54
My table files look like this:
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']
]);
I cannot understand why cascadeDelete
function is being used when this Table does not have any dependents.
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
If there were really only BelongsTo
associations on the table object where you are calling delete()
, then such a control flow should never happen, so there need to be more associations defined from elsewhere.
Given that the Ratings.Ratable
behavior belongs to dereuromark/cakephp-ratings
, I'd suspect that the problem are the conditions that are being defined for the HasMany
association that the behavior adds to the 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'
]
);
That value ends up being passed to array_merge()
as the second argument in DependentDeleteTrait::cascadeDelete()
, hence the error.
So, that's a bug in the plugin, and I'd suggest that you report it. As a quick workaround, you could overwrite the conditions in your table class, like
$this->association('Ratings')->conditions([]);