cakephppaginationthreaded-comments

CakePHP find('threaded') pagination


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.


Solution

  • 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