2016-06-10 84 views
0

CakePHP的查詢,這樣從控制器:爲什麼CakePHP 3.2 Paginator不能排序我的查詢結果?

$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']); 

這裏是debug($ratings)生產:

'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' 

    }, 

在視圖中,manufacturer排序工作正常。這是一個原始的數據庫字段。 count,maxmin值未被排序。

下面是該視圖的sort電話:

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

http://stackoverflow.com/questions/31763413/pagination-sort-link-on-a-virtual-field-entity-property-in-cakephp-3-0 – ndm

回答

1

你需要加入白名單,你要允許進行排序,如字段:Pagination

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

否則默認蛋糕3阻止它