【问题标题】:How to paginate the results from a Custom Finder in CakePHP 3如何在 CakePHP 3 中对自定义查找器的结果进行分页
【发布时间】:2020-01-11 09:13:36
【问题描述】:

如何在 CakePHP 3(特别是 3.7)中对来自自定义查找器的结果进行分页?

我有一个带有自定义查找器的模型:

// src/Model/Table/RevisionFiltersTable.php
public function findRegulatoryNotifications($date_start, $date_end, $regulation_id = 1, $u_id, $o_id, $count_only = false)
{

}

在这个查找器中,有一些方法可以使用 ORM 并执行查询,返回 $query->toArray()

目前我在 Controller 方法中手动调用它,如下所示:

$RevisionFilters = TableRegistry::getTableLocator()->get('RevisionFilters');
$data = $RevisionFilters->findRegulatoryNotifications(...);

它返回所有正确的数据,但不分页。例如,如果有 55 行,我会得到一个包含 55 个键的数组。

我传入的参数适合findRegulatoryNotifications()的签名

我在文档https://book.cakephp.org/3.0/en/controllers/components/pagination.html#using-controller-paginate 中阅读了有关使用分页和自定义查找器的信息

但我在 Controller 中看不到如何执行此操作。

文档以此为例:

$this->paginate = [
        'finder' => [
            'tagged' => $customFinderOptions
        ]
    ];

所以我假设我可以做类似的事情:

$RevisionFilters->paginate = [
        'finder' => [
            'regulatoryNotifications' => $customFinderOptions
        ]
    ];

我不明白在这种情况下应该将 $customFinderOptions 设置为什么。

假设我可以克服这个问题,我将如何将我的论点传递给findRegulatoryNotifications()

无法提供比这更多的代码,因为我不知道如何设置。

【问题讨论】:

    标签: pagination cakephp-3.0 cakephp-3.x


    【解决方案1】:

    这不是 finder 方法的样子,finder 只接受两个参数,第一个是查询本身,第二个是选项数组(传递给 Query::find() 的第二个参数),这就是 $customFinderOptions 在该示例中的含义,一个选项数组。

    你所拥有的只是一个 table 方法,如果你想使用它进行分页,该方法需要返回一个查询对象,你需要手动调用该方法并将其返回值传递给分页器。

    另见

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 2023-03-10
      • 2019-12-10
      • 2021-01-30
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 2017-09-15
      • 2011-03-24
      相关资源
      最近更新 更多