Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
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 13,511 times | 0 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
No comments. Be the first to post a reply.
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!