Tuesday, October 7, 2014

Problem with tree data - find('threaded') displays all, but getting threaded data from node (or using children() function) does not return all records

Hello.

I have a Comment model, which uses the Tree Behavior.
Comments are related to Post model, with the use of post_id field in comments table.

The problem is, as described in topic - when I use the find('threaded') function like this:

$comments=$this->Comment->find('threaded',
array(
'conditions' => array('post_id'=>$p['Post']['id']),
'fields' => array('Comment.id', 'parent_id', 'nick', 'comment', 'created'),
'order' => array('created'=>'asc')
));


everything works fine - all comments are existing in $comments array, as tree data.

The problem occurs when I want to get comments only from given parent_id node (so when I want to get subtree of comments).
I have to use this method (getting subtree of comments from parent comments), because I want my comments to be paginated.
To paginate comments I first paginate comments with parent_id=NULL, and then for each parent comment I get a threaded list of child comments:

So the simplified code looks like this:
- first - get the parent comments:

$this->paginate=array(
'conditions'=>array('parent_id'=>NULL,'post_id'=>$p['Post']['id']),
'fields'=>array('Comment.id','parent_id','lft','rght','nick','comment','created'),
'limit'=>$this->comments_num,
'order'=>array('Comment.created'=>'asc'),
'page'=>$page_num,
);
$comments=$this->paginate('Comment');

- second - get threaded child comments for each parent comment:

if($comments) {
foreach($comments as $key=>$value) {
$comments[$key]['children']=$this->Comment->find('threaded',
array(
'conditions'=>array('post_id' => $p['Post']['id'], 'lft >' => $value['Comment']['lft'], 'rght <' => $value['Comment']['rght']),
'fields'=>array('Comment.id','parent_id','nick','comment','created'),
'order'=>array('Comment.created'=>'asc')
)
);
}
}


But the problem is, the subtree of comments does not contain all the threaded children.

I checked some of the comments that didn't show up in a subtree and it turns out that their "rght" field value is not smaller than "rght" or parent, but it is a bigger value.

What caused that situation? Why find('threaed') of whole comments table returns all the nodes and comments correcty, but getting a subtree fails on some records (the ones that have the "rght" value bigger than parent "rght" value)?

I also tried to do this in a way to first get all the childrens ids of a parent node, and then get all threaded comments with conditions containing 'Comment.id' => '$children_ids.
But the $children_ids array is being created on the basis of Tree Behavior childen() function, which also does not return all the records (again, the ones with higher "rght" values are not displayed).

Some one please help :-))


--
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: