0

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>
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Matt
  • 15
  • 5
  • http://stackoverflow.com/questions/31763413/pagination-sort-link-on-a-virtual-field-entity-property-in-cakephp-3-0 – ndm Jun 10 '16 at 13:51

1 Answers1

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