MainelyDesign.com Blog

Delete Dependent Just Deleted All My Records

Posted on 09/17/2009 at 10:12 am by Kevin Wentworth
Viewed 2,486 times | 8 comments

Oh man! I just deleted all my database records using $model->del($id) with dependent=>true in the model association.  That wasn't supposed to happen!  It turns out to be an issue with how I specified my model association.  What's weird is that everything works fine except when I go to do the delete with cascade set to true.

Use an array for conditions when associating models

I was using some older model code and never changed it because everything worked fine.  However, the issue came down to not using an array for my conditions within the model association.  I have a model gallery that hasMany images and is dependent.  Below is the old code and the new code:

Bad code (note the conditions string):

  1. class Gallery extends AppModel {
  2.  
  3.     var $name = 'Gallery';
  4.     var $hasMany = array(
  5.         'Image' => array
  6.         (
  7.             'className' => 'Image',
  8.             'order' => 'Image.sort_order ASC',
  9.             'foreignKey' => 'type_id',
  10.             'conditions' => "Image.type = 'gallery'", //BAD!
  11.             'dependent' => true
  12.         ),
  13.     );  
  14.    
  15. }

Good code (note the conditions array):

  1. class Gallery extends AppModel {
  2.  
  3.     var $name = 'Gallery';
  4.     var $hasMany = array(
  5.         'Image' => array
  6.         (
  7.             'className' => 'Image',
  8.             'order' => 'Image.sort_order ASC',
  9.             'foreignKey' => 'type_id',
  10.             'conditions' => array('Image.type' => 'gallery'), //GOOD!
  11.             'dependent' => true
  12.         ),
  13.     );  
  14.    
  15. }

Array_merge() error in model.php at line 1763

The issue is that CakePHP wants to merge the conditions array with the existing conditions.  That's why your model association conditions MUST be an array.

Cheers,

-Kevin Wentworth

 

Bookmark and Share

Tags for Delete Dependent Just Deleted All My Records

Cakephp | Web Programming | Mysql | Usage | Database

Comments for this Posting

Posted by narnia

on 10/12/10

great post, just the kind of information I was looking for

http://www.youtube.com/watch?v=oryKVZfix_Y

Posted by PXStevey

on 24/1/11

Hi, I'm new I would like to welcome all... :)

http://filesarchive.wroclaw.pl/

Posted by Poker Gratis

on 17/2/11

Texto prazeroza nesta página, visões como aqui está dão valor a quem reflectir neste blogue .....
Realiza muito mais do teu blogue, aos teus utilizadores.

http://www.pokersemdeposito.com/

Posted by Ganhar Dinheiro na Internet

on 23/2/11

Oi agradável este site está muito organizado.........bom estilo:)
Muito Bonito Continua assim !

http://www.ganhardinheiroonlinefacil.blogspot.com/

Posted by custom hockey jerseys

on 2/3/11

I like the products of your blog, maybe u will like these things of my blog!

http://www.enfljerseys.com/

Posted by buy winstrol

on 3/3/11

I just added your web page to my favorites. I like reading your posts. Thanks!

http://anabolmarket.net/

Posted by refrigerator repair orange county

on 8/3/11

How come you dont have your site viewable in mobile format? Can not view anything in my netbook.

http://www.ccappliancerepair.com/

Posted by Minecraft Guide

on 10/3/11

It's interesting to see this point of view. I can't say fore sure if I agree or not, but it is something I will think about now.

http://minecraftresource.com/minecraft-guide/

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design