如何在 CakePHP 3 中使用分页器对外部字段进行排序

How to sort external fields using Paginator in CakePHP 3

我正在使用 cakephp 3.0 开发应用程序。 我想知道分页器是否允许对来自 'contain' 子句中包含的其他 table 的字段进行排序。 例如我有这个声明:

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

table 'Actors' 包含字段 'description'。是否可以按 table 演员的描述字段排序?

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

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

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

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

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

正如 José Lorenzo 在这里所说 ,Paginator 阻止了主 table 中不可用的每个字段。 因此,如果您需要通过外部字段进行排序,则有必要指定 cakephp 在连接中使用的完整限定名称 table.

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

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

并在视图中使用此表达式:

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

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