Matt
Matt

Reputation: 15

Why won't CakePHP 3.2 Paginator sort on my query results?

CakePHP query and such from the controller:

$ratings = $this->Ratings->find('all')->group('manufacturer');
$ratings->select(['manufacturer',
                  'count' => $ratings->func()->count('*'),
                  'max' => $ratings->func()->max('psi_score'),
                  'min' => $ratings->func()->min('psi_score')]);
$ratings = $this->paginate($ratings);
$this->set(compact('ratings'));
$this->set('_serialize', ['ratings']);

Here is what debug($ratings) produces:

'items' => [
    (int) 0 => object(Cake\ORM\Entity) {

        'manufacturer' => 'A10Green Technology',
        'count' => (int) 8,
        'max' => '7.41',
        'min' => '6.57',
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Ratings'

    },

In the view, sorting on manufacturer works fine. That is an original db field. The count, max and min values are not getting sorted.

Here are the sort calls for the view:

<th><?= $this->Paginator->sort('manufacturer')?></th>
<th><?= $this->Paginator->sort('count')?></th>
<th><?= $this->Paginator->sort('max')?></th>
<th><?= $this->Paginator->sort('max')?></th>

Upvotes: 0

Views: 126

Answers (1)

You need to whitelist the fields that you want to allow to be sorted like in: Pagination

public $paginate = [
    'sortWhitelist' => [
        'id', 'title', 'Users.username', 'created'
    ]
];

Otherwise by default cake 3 blocks it

Upvotes: 1

Related Questions