Re: tree behaviour and validation rule problem
On 2 Feb, 15:45, John Andersen wrote: > As I can't see your full code from the moment you retrieve the > Category and until you try to save it, I can't be sure that you have > provided the Category.id at the time of saving the updated parent_id. > > I just imagine that CakePHP actually may try to add a new record, in > which case the isUnique should fail. I developed my code starting from here: http://realm3.com/articles/drag_and_drop_trees_with_cakephp At the end of the page there's a "save" function and my code is almost equal to that one. $category = $this->Category->find('first', array('conditions' => array ( 'Category.id' => $this_node_id) )); $this->Category->set($category); if (isset($category['Category']['id']) && $category['Category'] ['parent_id'] !== $this_node_parent_id) { $ret = $this->Category->save(array('parent_id' => $this_node_parent_id)); (the names of the variables are obvious.) The original code used SetParent that, as you know, is deprecated now. I didn't use $this->Category->id = 1234 before $this->Category->save () because I presumed that find('first') would have the same effect with the ActiveRecord approach. But I could be totally wrong on this. And because, before adding the isUnique validation rule, my "save" function did work. Anyway, I just added $this->Category->id=1234; before $this->Category- >save() and the problem is still there. :-( Thank you maxx Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: tree behaviour and validation rule problem
As I can't see your full code from the moment you retrieve the Category and until you try to save it, I can't be sure that you have provided the Category.id at the time of saving the updated parent_id. I just imagine that CakePHP actually may try to add a new record, in which case the isUnique should fail. Does this help? Enjoy, John On Feb 2, 11:09 am, emmexx wrote: > I wrote a simple model that acts as a tree behaviour. > The model has a myfield_id field that must follow these rules: > - if parent_id is empty (root) myfield_id can't be blank > - if parent_id is not empty myfield_id must be blank (children) > > So I wrote a isUnique validation rule for myfield_id. And it seems to > works if I use the baked add and edit pages. > > Then I created another page that uses a javascript widget (tafelTree) > to show the tree and to drag and drop around nodes. > In the controller I created a function that receives the id and the > (new) parent id of a node as a parameter. If node id 1234 with > parent_id 5432 becomes (in javascript) a children of node 8765 my > function receives 1234-8765. > I retrieve the record with: > $category = $this->Category->find('first', > array('conditions' => array('Category.id' =>1234)) > ); > and try to save it with: > $ret = $this->Category->save(array('parent_id' => 8765)); > > This fails with a validation error for the isUnique rule. > I put a log line in the isUnique function (in cake/lib.../model.php) > to see what is the "condition" that creates the error and... > > ( > [or] => Array > ( > [Category.myfield_id] => > ) > > [Category.id !=] => 1234 > ) > > But this is the same condition that I found when saving that record > using the edit function (with no error). > So I don't understand why there's a validation error when the > condition is the same. > > Any suggestion? > > Thank you > > maxx > > p.s. En passent... the childrenCount function doesn't work properly > with my data. It reports a number of nodes greater than the real > number. I "verified" and "reordered" my data but the results of the > function are still wrong. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
tree behaviour and validation rule problem
I wrote a simple model that acts as a tree behaviour. The model has a myfield_id field that must follow these rules: - if parent_id is empty (root) myfield_id can't be blank - if parent_id is not empty myfield_id must be blank (children) So I wrote a isUnique validation rule for myfield_id. And it seems to works if I use the baked add and edit pages. Then I created another page that uses a javascript widget (tafelTree) to show the tree and to drag and drop around nodes. In the controller I created a function that receives the id and the (new) parent id of a node as a parameter. If node id 1234 with parent_id 5432 becomes (in javascript) a children of node 8765 my function receives 1234-8765. I retrieve the record with: $category = $this->Category->find('first', array('conditions' => array('Category.id' =>1234)) ); and try to save it with: $ret = $this->Category->save(array('parent_id' => 8765)); This fails with a validation error for the isUnique rule. I put a log line in the isUnique function (in cake/lib.../model.php) to see what is the "condition" that creates the error and... ( [or] => Array ( [Category.myfield_id] => ) [Category.id !=] => 1234 ) But this is the same condition that I found when saving that record using the edit function (with no error). So I don't understand why there's a validation error when the condition is the same. Any suggestion? Thank you maxx p.s. En passent... the childrenCount function doesn't work properly with my data. It reports a number of nodes greater than the real number. I "verified" and "reordered" my data but the results of the function are still wrong. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en