如何在CakePHP 3中使用Paginator对外部字段进行排序 [英] How to sort external fields using Paginator in CakePHP 3

查看:63
本文介绍了如何在CakePHP 3中使用Paginator对外部字段进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cakephp 3.0开发应用程序。
我想知道分页器是否允许对 contain子句中包含的其他表中的字段进行排序。
例如,我有以下语句:

I'm developing an application using cakephp 3.0. I'm wondering if the paginator allows to sort fields coming from other tables which are included in the 'contain' clause. For example I have this statement:

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

演员表中包含说明字段。

The table 'Actors' contains the field 'description'. Is it possible to sort by the description field of the table Actors?

我尝试使用点表示法指定字段:

I tried to specify the field using the dot notation:

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

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

as well as to include the model within the option array:

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

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

In both cases it doesn't seem to work. These two approaches come from cakephp 2.0 where everything works fine (cakephp Paginator -> sort - model option). Apparently in the new documentation there aren't any updates.

推荐答案

正如JoséLorenzo在这里所说的在Cakephp 3.x中进行分页排序,分页器会阻止每个不可用的字段在主表中。
因此,如果需要按外部字段进行订购,则必须在连接表中指定cakephp使用的完整合格名称。

As José Lorenzo says here Pagination Sort in Cakephp 3.x, Paginator blocks each field which is not available in the primary table. Thus if you need to order by a foreign field it is necessary to specify the full qualified name used by cakephp in the joining table.

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

In my example I need to specify the following:

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

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

and use this expression in the view:

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

通过这种方式,所有事物都像魅力一样。如果您找不到正确的字段,则可以启用查询调试功能,并查看查询中使用的字段。

In this way everything works like a charm. If you are not able to identify the right field you may enable the query debug and see which field is used in the query.

这篇关于如何在CakePHP 3中使用Paginator对外部字段进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆