Sort query with cakephp paginate 3.0

Asked

Viewed 301 times

1

 $this->paginate = [
       'contain' => ['Unidades'],
       'fields' =>['PontoDeColetas.id','PontoDeColetas.nome','PontoDeColetas.status','Unidades.id','Unidades.nome'],
       'order' => ['Unidades.nome' => 'desc']
 ];

As I show above, I am trying to arrange by the name of a daughter class, but it doesn’t work. Could someone help me?

3 answers

0


$this->paginate = [
        'contain' => ['Unidades'],
        'fields' => ['PontoDeColetas.nome','PontoDeColetas.status','Unidades.id','Unidades.nome'],
        'sortWhitelist'  =>  ['nome','status','Unidades.nome'],
        'order' => ['status' => 'desc','nome' => 'asc']
        ];

I solved with this line 'sortWhitelist'.

  • Perfect, it seems to be a new feature of Cakephp 3, I didn’t know. You can mark your own answer as the correct one in this case.

0

$options = array(
              'contain' => array('Unidades')
              'fields' => array('PontoDeColetas.id','PontoDeColetas.nome','PontoDeColetas.status','Unidades.id','Unidades.nome'),
              'order' => array('Unidades.nome' => 'desc'),


            );

$this->paginate = $options;

Try this.

0

When using contain, the Cakephp performs several darlings, as many as are included. Then you cannot perform conditions, sort, group and etc in your main class from the contain.

So if your class that will be paginated is the Pontodecoletas, she’s the one you can order in the key order that you put in your code.

Now, you can order Units which are contained in Pontodecoletas, which includes only the order, thus:

$this->paginate = [
    'contain' => ['Unidades' => ['order' => ['Unidades.nome' => 'DESC']]],
    'fields' => ['PontoDeColetas.id','PontoDeColetas.nome','PontoDeColetas.status','Unidades.id','Unidades.nome']
];

Otherwise, you’ll have to use joins, then the solution is a little different.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.