CakePHP paginate and order by

后端 未结 4 779
南方客
南方客 2021-01-15 03:10

It feels like I\'ve tried everything so I now come to you.

I am trying to order my data but it isn\'t going so well, kinda new to Cake.

This is my code:

4条回答
  •  甜味超标
    2021-01-15 03:55

    You need to pass in the conditions key when using multiple filters (i.e. order, limit...). If you just specify conditions, you can pass it as second parameter directly.

    This should do it:

    $this->set('threads', $this->paginate('Thread', array(
            'conditions' => array(
                'Thread.hidden' => 0,
                'Thread.forum_category_id' => $id
            ),
            'order' => array(
                'Thread.created' => 'desc'
            )
        )));
    

    or perhaps a little clearer:

    $this->paginate['order'] = array('Thread.created' => 'desc');
    $this->paginate['conditions'] = array('Thread.hidden' => 0, ...);
    $this->paginate['limit'] = 10;
    $this->set('threads', $this->paginate());
    

    if you get an error, add public $paginate; to the top of your controller.

提交回复
热议问题