02 7 / 2011
How to fix “Item was not deleted” in CakePHP 1.3 and delete all associated records
When attempting to delete a record that has related records, for example a User hasMany Comments, CakePHP 1.3 would not delete the item in my baked application. It was giving me this error:
Item was not deleted
The problem is caused by the fact that there are related records. Some Comments still belongTo a User, so you can not delete the User without deleting the Comment. A simple edit to the User model can solve this problem:
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'dependent' => true,
)
);
The line to pay attention to there is ‘dependent’ => true. By default this is set to false, by simply changing this, you can delete all related records at the same time as the main record automagically.
Permalink 9 notes