cakephp delete casade with sub-categories -
using cake 2.x have 3 tables (i've shortened names example removing plural/singular cakephp conventions):
- main (has many sub1)
- sub1 (belongs main, hasmany sub2)
- sub2 (belongs sub1)
when delete category main, of dependent sub1/sub2 items deleted using $this->main->delete($id, true) call.
however i'm not sure how delete item sub1 , remove it's sub2 properties. i'm new cakephp bare me. controller needs manipulate 3 levels of tables. tried:
$this->loadmodel('sub1'); $res = $this->sub1->delete($id, true);
but not picking model bindings sub2 , deleting them. proper convention doing kind of thing in cake and/or doing wrong?
there's 2 things i'd suggest checking:
1) check dependent
set true both associations, is, $hasmany in main, , $hasmany in sub1
2) check 'exclusive' not set true in either of associations. cookbook:
when exclusive set true, recursive model deletion delete deleteall() call, instead of deleting each entity separately. improves performance, may not ideal circumstances.
if dependent
true, , exclusive
false, both associations, cake should delete associations recursively, can see source code.
lastly, couple of small tips keep in mind, though unrelated current problem:
a)
in code snippet gave controller, don't need load sub1 model. it's available. can call:
$res = $this->main->sub1->delete($id, true);
b) no need pass in true
second param in delete
method, because it's default value true
. see http://book.cakephp.org/2.0/en/models/deleting-data.html
c) don't know specific differences between main, sub1 , sub2 models. if they're same thing , you're wanting keep track of hierarchy, should tree behaviour
Comments
Post a Comment