CakePHP paginate and order by

后端 未结 4 795
南方客
南方客 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:53

    There are a few things to take note of in paginate with order. For Cake 3.x, you need :

    1) Ensure you have included the fields in 'sortWhitelist'

    $this->paginate = [
    
            'sortWhitelist' => [
                'hidden', 'forum_category_id', 
            ],
    
    
        ];
    

    2) for 'order', if you put it under $this->paginate, you will not be able to sort that field in the view. So it is better to put the 'order' in the query (sadly this wasn't stated in the docs)

    $query = $this->Thread->find()
    ->where( ['Thread.hidden' => 0, 'Thread.forum_category_id' => $id, ] )
    ->order( ['Thread.created' => 'desc'] );
    
    $this->set('threads', $this->paginate($query) 
    

提交回复
热议问题