【问题标题】:Pagination on custom Array in CakePHP 3CakePHP 3中自定义数组的分页
【发布时间】:2016-01-12 00:55:08
【问题描述】:

我有自定义数组,例如:

[business] => Array
(
    [55] => Array
    (
        [id] => 1
        [name] => abc
        [contact] => 1325467897
    ),
    [96] => Array
    (
        [id] => 5
        [name] => xyz
        [contact] => 9876543210
    )

)

此数组派生自条件(if-else)多个查询。所以,我只想在数组上添加分页。有人可以建议我如何使用 CakePHP 3 的标准分页器在这种类型的数组上添加自定义分页。

【问题讨论】:

标签: php pagination cakephp-3.0 paginator cakephp-3.1


【解决方案1】:

PaginatorComponent 可以采用Cake\ORM\Query 作为分页的东西,所以我建议将您编译的查询对象传递给分页器。

因此,在您的控制器中,您可以执行以下操作。

public function example()
{
    $this->paginate = ['limit' => 5];

    $query = $this->Examples->find();

    if ($something === 'foo') {
        $query->where(['foo' => $something]);
    }

    if ($wtfbbq) {
        $query->contain(['Wtfs', 'Barbeques']);
    }

    $results = $this->paginate($query);
}

这将产生一个带有内置分页的Cake\ORM\Query。然后您可以根据需要调整分页参数by checking the book page on the Paginator

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 2015-06-19
    • 2019-12-10
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多