Sort query with paginate cakephp 3.0

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'm trying to organize by the name of a child class, but it does not work. Could someone help me?

    
asked by anonymous 19.02.2016 / 12:51

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 resolved with this line 'sortWhitelist'.

    
22.02.2016 / 18:43
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.

    
19.02.2016 / 13:43
0

When using contain , CakePHP performs several queries , as many as are included. So you can not perform conditions, sort, group, and so on in your main class from contain classes.

So if your class that will be paged is the PointCollect , it's the one you can sort in the order key you placed in your code.

Now you can sort Units that are contained in PointCollect , which is to include the order , like this:

$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 . , that's a bit different from the solution.

    
19.02.2016 / 14:23