ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # 分页 当我们需要逐渐呈现大量任意数据时,就会发生分页过程。`Phalcon\Paginator`提供了一种快速便捷的方法,可将这些数据集拆分为可浏览页面。 ## 数据适配器 该组件使用适配器封装不同的数据源: | 适配器 | 描述 | | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `Phalcon\Paginator\Adapter\NativeArray` | 使用PHP数组作为源数据 | | `Phalcon\Paginator\Adapter\Model` | 使用`Phalcon\Mvc\Model\Resultset`对象作为源数据。由于PDO不支持可滚动游标,因此不应使用此适配器对大量记录进行分页 | | `Phalcon\Paginator\Adapter\QueryBuilder` | 使用`Phalcon\Mvc\Model\Query\Builder`对象作为源数据| ## 工厂 使用`adapter`选项加载Paginator Adapter类 ```php <?php use Phalcon\Paginator\Factory; $builder = $this->modelsManager->createBuilder() ->columns('id, name') ->from('Robots') ->orderBy('name'); $options = [ 'builder' => $builder, 'limit' => 20, 'page' => 1, 'adapter' => 'queryBuilder', ]; $paginator = Factory::load($options); ``` ## 示例 在下面的示例中,分页器将使用模型中的查询结果作为其源数据,并将显示的数据限制为每页10条记录: ```php <?php use Phalcon\Paginator\Adapter\Model as PaginatorModel; // Current page to show // In a controller/component this can be: // $this->request->getQuery('page', 'int'); // GET // $this->request->getPost('page', 'int'); // POST $currentPage = (int) $_GET['page']; // The data set to paginate $robots = Robots::find(); // Create a Model paginator, show 10 rows by page starting from $currentPage $paginator = new PaginatorModel( [ 'data' => $robots, 'limit' => 10, 'page' => $currentPage, ] ); // Get the paginated results $page = $paginator->getPaginate(); ``` `$currentPage` 变量控制要显示的页面。`$paginator->getPaginate()` 返回包含分页数据的`$page`对象。它可以用于生成分页: ```php <table> <tr> <th>Id</th> <th>Name</th> <th>Type</th> </tr> <?php foreach ($page->items as $item) { ?> <tr> <td><?php echo $item->id; ?></td> <td><?php echo $item->name; ?></td> <td><?php echo $item->type; ?></td> </tr> <?php } ?> </table> ``` `$page` 对象还包含导航数据: ```php <a href='/robots/search'>First</a> <a href='/robots/search?page=<?= $page->before; ?>'>Previous</a> <a href='/robots/search?page=<?= $page->next; ?>'>Next</a> <a href='/robots/search?page=<?= $page->last; ?>'>Last</a> <?php echo 'You are in page ', $page->current, ' of ', $page->total_pages; ?> ``` ## 使用适配器 每个适配器必须使用的源数据示例: ```php <?php use Phalcon\Paginator\Adapter\Model as PaginatorModel; use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray; use Phalcon\Paginator\Adapter\QueryBuilder as PaginatorQueryBuilder; // Passing a resultset as data $paginator = new PaginatorModel( [ 'data' => Products::find(), 'limit' => 10, 'page' => $currentPage, ] ); // Passing an array as data $paginator = new PaginatorArray( [ 'data' => [ ['id' => 1, 'name' => 'Artichoke'], ['id' => 2, 'name' => 'Carrots'], ['id' => 3, 'name' => 'Beet'], ['id' => 4, 'name' => 'Lettuce'], ['id' => 5, 'name' => ''], ], 'limit' => 2, 'page' => $currentPage, ] ); // Passing a QueryBuilder as data $builder = $this->modelsManager->createBuilder() ->columns('id, name') ->from('Robots') ->orderBy('name'); $paginator = new PaginatorQueryBuilder( [ 'builder' => $builder, 'limit' => 20, 'page' => 1, ] ); ``` ## 页面属性 The `$page` 对象具有以下属性: | 属性 | 描述 | | ------------- | ------------------------------------------------------ | | `items` | 要在当前页面显示的记录集 | | `current` | 当前页面 | | `before` | 当前的上一页 | | `next` | 当前的下一页 | | `last` | 记录集中的最后一页 | | `total_pages` | 页数 | | `total_items` | 源数据中的项目数 | ## 实现自己的适配器 必须实现 `Phalcon\Paginator\AdapterInterface` 接口才能创建自己的分页器适配器或扩展现有的分页器适配器: ```php <?php use Phalcon\Paginator\AdapterInterface as PaginatorInterface; class MyPaginator implements PaginatorInterface { /** * Adapter constructor * * @param array $config */ public function __construct($config); /** * Set the current page number * * @param int $page */ public function setCurrentPage($page); /** * Returns a slice of the resultset to show in the pagination * * @return stdClass */ public function getPaginate(); } ```