Recent Posts
- (02/24) Calling an Element from a Helper TAGS:CakephpWeb ProgrammingUsageTutorialExample
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
Subscribe to my feed
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):
- class Gallery extends AppModel {
- var $name = 'Gallery';
- (
- 'className' => 'Image',
- 'order' => 'Image.sort_order ASC',
- 'foreignKey' => 'type_id',
- 'conditions' => "Image.type = 'gallery'", //BAD!
- 'dependent' => true
- ),
- );
- }
Good code (note the conditions array):
- class Gallery extends AppModel {
- var $name = 'Gallery';
- (
- 'className' => 'Image',
- 'order' => 'Image.sort_order ASC',
- 'foreignKey' => 'type_id',
- 'dependent' => true
- ),
- );
- }
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
Tags for Delete Dependent Just Deleted All My Records
Cakephp | Web Programming | Mysql | Usage | Database
Comments for this Posting
Posted by PXStevey
on 24/1/11
Hi, I'm new I would like to welcome all... :)
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.
Posted by Ganhar Dinheiro na Internet
on 23/2/11
Oi agradável este site está muito organizado.........bom estilo:)
Muito Bonito Continua assim !
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!
Posted by buy winstrol
on 3/3/11
I just added your web page to my favorites. I like reading your posts. Thanks!
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.
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.
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!

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