1

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.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Sophia Gavish
  • 467
  • 2
  • 8
  • 16

5 Answers5

1

You have to do it manually in controller by paginate.

The key is properly order mysql query to find comment not by id by firstly by parent_id and then id (columns are just example) - that would work for 2 level comments:

    $this->paginate = array(
            'recursive' => -1,
            'conditions' => array(
                'Comment.blog_id' => 0,
            ),
            'order' => array('parent_id', 'id'),
            'limit' => 10   
    );
    $this->set('comments', $this->paginate('Comment'));
Jacek Kaniuk
  • 5,229
  • 26
  • 28
0

You can achieve this as

Your modal code:

var $hasMany = array(
    'Pages' => array(
        'className' => 'Page',
        'foreignKey' => 'parent_id',
    )
);

Your Controller code:

$this->paginate = array(
        'Page' => array(
            'contain' => array(
                'Pages'
            ),
            'conditions' => array(
                'Page.parent_id' => null
            )
        )
    );
    $pages = $this->paginate();
Farhan
  • 1,561
  • 14
  • 12
0

You should use tree behavior and order by lft.

lenivo
  • 1
0

You can't. What I did is a link which switched between full threaded comments view AND flat paginated list.

Sergei
  • 2,747
  • 1
  • 17
  • 16
  • And if I will use two tables? one for root and second for threaded comments? Maybe then I can paginate the root but the problem will be to get the data from the database. I don't want to split a thread into separate pages. – Sophia Gavish Apr 30 '11 at 16:39
  • Paginating threaded comments is really bad idea, and splitting the table too. However you can try to make collapsable/expandable threads. – Sergei May 01 '11 at 01:48
0

IMHO: do you think that paginate comments is a good idea? just minimize space comments take on the page, and put them into one float. don't make your user to click too many links...

Aziz
  • 859
  • 6
  • 16
  • 1
    Every comment has a title and content, the user will click on the titles to see the content. This will save space and leave the user on the page. – Sophia Gavish Apr 30 '11 at 16:41