CakePHP find('threaded') pagination

2.2k Views Asked by At

I'm kind of new to cakePHP and get to the moment where i have to do pagination.

The comments table has a parent_id and the threaded query is working correctly so now, I want to paginate the results.

My problem is the limited sql query affects all the retrieved comments and I only want to limit the parent ones because the other way, it leaves replies out of the query.

Hope I'd be clear and you can help me.

Thanks.

2

There are 2 best solutions below

0
Gunnar Oledal On BEST ANSWER

Use:

    var $hasMany = array(
            'ChildComment' => array(
                    'className' => 'ProfileComment',
                    'foreignKey' => 'parent_id',
                    'conditions' => '',
                    'dependent' => true,
                    'fields' => '',
                    'order' => 'created ASC'
            )
    );

        var $belongsTo = array(
            'ParentComment' => array(
                    'className' => 'ProfileComment',
                    'foreignKey' => 'parent_id',
                    'conditions' => '',
                    'fields' => '',
                    'order' => ''
            ));

and then in the find:

$comments = $this->User->ProfileComment->find('all', array(
                'limit' => $this->incNumber,
                'offset' => $page*$this->incNumber,
                'conditions' => array('ProfileComment.parent_id' => null, 'ProfileComment.user_id' => $id),
                'order' => 'ProfileComment.created DESC'
            ));

You have to customize the code for your purpose, but the keypoint is the relations and that the find condition has parent_id = null. This way the limit only affects the parents

0
Leo On

You probably need to do the query for just the parents (topics?) then another query for each tree below that.