1

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.

ozzysong
  • 140
  • 1
  • 6
  • 1
    I'm afraid I don't understand your question. Please clarify the "My problem is the limited..." sentence and I'll do my best to assist. Provide basic model structure (hasOne, etc.) as well as include the array structure you expect, along with the array structure it's giving you. THanks! – Travis Leleu Dec 23 '09 at 17:48
  • By limited I mean I want to limit the query to 10 results, but this limit affects the replies too. – ozzysong Jan 15 '10 at 09:51

2 Answers2

2

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

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
0

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

Leo
  • 6,553
  • 2
  • 29
  • 48