Is there a way to paginate comments in cakePHP with a tree behavior? Should I use tree behavior anyway or to make my own code to view the comments?
Sorry, this has been asked before. I found an article: Managing Hierarchical Data in MySQL
...and I will post my solution.
Nope, I didn't find a good solution. I don't want to reinvent the wheel, I do want to make it in the cake way.
I wrote a helper for recursively print the comments:
# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){
if (empty($comments) || !is_array($comments)) return false;
echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
if (is_array($comments)){
foreach($comments as $comment):
?>
<li id="<?php echo $comment['Forum']['id']; ?>">
<div class="inner">
<h4><?php echo $comment['Forum']['title']; ?></h4>
<div class="meta">
<?php echo $comment['User']['first_name']; ?>
</div>
<div class="content">
<?php echo $comment['Forum']['content']; ?>
</div>
</div>
<?php
if (isset ($comment['children']))
if (is_array($comment['children'])){
if (!empty($comment['children'])) $this->printComments($comment['children']);
}
echo '</li>';
endforeach;
}
else{
echo '<li>'.$comment['Forum']['title'].'</li>';
}
echo '</ul>';
}
Here is how I retrieve the data from the database:
# controllers/forums.php
$this->set('tree', $this->Forum->find('threaded'););
The problem is how to paginate the comments?
Currently I'm using 2 tables, one for the root and 2nd for the threaded comments. I didn't find a solution.