On Tuesday, 20 August 2013 16:25:40 UTC+2, Mohammad Naghavi wrote: > > Hi everybody, > I have got a problem that I'm fighting with for a while so I just thought > I would ask you for some advice. > I have a tree with parent, child relation. each element on this tree has a > relation with another model. what I need is when an entry in the tree is > saved, following happens: > * first: if it has a parent the relation to the second model should be > inherited from the parent (not always but under some conditions) > * second: if the relation to the second model is changed (based on the > above condition) then set the same relation for the children > > I have tried to do this on the beforeSave. there I can successfully > fulfill the first statement above but when I want to set the realtion for > children I cant do it efficiently because it is a waterfall effect and not > all the children are direct children and basically I have to find a > threaded list and go through all of them and change this this realtion for > all of them. > > I want to know if there is a possible way to automatically trigger the > beforeSave for all the children if the relation to second model is changed > for one single object? >
Instead of calling save/saveField in a loop - you can use updateAll http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions To update all children in one sql command e.g. function afterSave() { if (your logic here) { $this->updateAll( array('x_id' => $x), array('lft >' => $this->data[$this->alias]['lft'], 'rght <' => $this->data[$this->alias]['rght']) ); } } AD > -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/groups/opt_out.
