【问题标题】:How to sort external fields using Paginator in CakePHP 3如何在 CakePHP 3 中使用 Paginator 对外部字段进行排序
【发布时间】:2015-08-10 09:44:21
【问题描述】:

我正在使用 cakephp 3.0 开发一个应用程序。 我想知道分页器是否允许对来自“包含”子句中包含的其他表的字段进行排序。 例如我有这样的说法:

    $this->paginate = [
        'contain' => ['Actors']
    ];

“演员”表包含“描述”字段。是否可以按Actors表的描述字段排序?

我尝试使用点符号指定字段:

$this->Paginator->sort('Actors.description', __('Description'))

以及将模型包含在选项数组中:

$this->Paginator->sort('description', __('Description'), ['model' => 'Actors'])

在这两种情况下,它似乎都不起作用。这两种方法来自 cakephp 2.0,一切正常 (cakephp Paginator -> sort - model option)。显然在新文档中没有任何更新。

【问题讨论】:

标签: cakephp-3.0


【解决方案1】:

正如 José Lorenzo 在这里 Pagination Sort in Cakephp 3.x 所说,Paginator 会阻止主表中不可用的每个字段。 因此,如果您需要按外部字段排序,则需要在连接表中指定 cakephp 使用的完整限定名。

在我的示例中,我需要指定以下内容:

$this->paginate = [
    'contain' => ['Actors'],
    'sortWhitelist'=>['Actors.description']
];

并在视图中使用这个表达式:

<?= $this->Paginator->sort('Actors.description', __('Description')) ?>

通过这种方式,一切都像魅力一样运作。如果您无法识别正确的字段,您可以启用查询调试并查看查询中使用了哪个字段。

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    相关资源
    最近更新 更多