I have a table called custom_carts and I have a table called custom_cart_items.
custom_cart_items has a foreign key called custom_cart_id that is set to custom_cart.id
According to the Cake Manual, when you call Model->delete() the first param is the id of the entry you want to delete and the second param is whether or not to cascade delete the dependent entries.
So when I call $this->CustomCart->delete(7,true) I get this error:
SQL Error: 1451: Cannot delete or
update a parent row: a foreign key
constraint fails
(`krake`.`custom_cart_items`,
CONSTRAINT `custom_cart_items_ibfk_1`
FOREIGN KEY (`custom_cart_id`)
REFERENCES `custom_carts` (`id`))
Here is the query:
DELETE `CustomCart`
FROM `custom_carts` AS `CustomCart`
LEFT JOIN `users` AS `User`
ON (`CustomCart`.`user_id` = `User`.`id`)
WHERE `CustomCart`.`id` = 25
Shouldn’t it cascade and dlete the other entry too?
So why am I getting an error?
You must set the dependent parameter to true in the model with the hasmany relationship in order to enable cascading deletion.
Your model would look something like this:
Please see http://book.cakephp.org/view/82/hasMany