【问题标题】:Why won't CakePHP 3.2 Paginator sort on my query results?为什么 CakePHP 3.2 Paginator 不对我的查询结果进行排序?
【发布时间】:2016-10-11 12:04:25
【问题描述】:

来自控制器的 CakePHP 查询等:

$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 上的排序工作正常。那是一个原始的数据库字段。 countmaxmin 值未排序。

这里是视图的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>

【问题讨论】:

标签: sorting cakephp cakephp-3.x


【解决方案1】:

您需要将您希望允许排序的字段列入白名单:Pagination

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

否则默认蛋糕 3 块它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    相关资源
    最近更新 更多