0
votes

I have a question regarding the Paginator Component of CakePHP3 (3.0.13). I'm using the MYSQL function FIELD() to order my data, like that:

$this->paginate = array_merge_recursive([
      'conditions' => [
           'zip IN' => $zips
      ],
           'order' => [
                  'FIELD(zip, '.rtrim(implode(',', $zips), ',').')',
                  'ispro' => 'desc',
           ],
           $this->paginate
     ]); 

When I apply this to my $this->paginate() it wouldn't be recognized as long as the query param sort is set. To avoid this I remove the sort in my request with:

if (isset($this->request->query['sort'])) {
      unset($this->request->query['sort']);
}

This is working, but I was wondering if there is a better solution, maybe in the paginator component itself, which I haven't found yet.

1
please mention your cakephp version. Also post the code where you use FIELD() function - arilia
I'm using cake 3.0.13. - Seb
I guess you should add your calculated field to sortWhitelist option - arilia
You mean like $this->paginate('sortWhitelist' =>[ 'zip']);? Already tried that, but it wouldn't work. - Seb
you are creating a new calculated field. So you should do $this->paginate('sortWhitelist' =>[ 'FIELD(zip, '.rtrim(implode(',', $zips), ',').')']) - arilia

1 Answers

2
votes

you can sort your query before you paginate it. Your sort conditions will come before the one appended from the paginator component

so you can do

$dentists = $this->Dentists->find()
    ->order([
        'FIELD(zip, '.rtrim(implode(',', $zips), ',').')',
        'ispro' => 'desc'
     ]);

$this->paginate = array_merge_recursive([
    'conditions' => [
        'zip IN' => $zips
    ],
    $this->paginate
 ]); 

$dentists = $this->paginate($dentists);