分页Pagination

分页的过程发生在我们需要逐渐呈现大组的任意数据。Phalcon\Paginator提供快速和方便的方式将这些数据集输出到页面。

The process of pagination takes place when we need to present big groups of arbitrary data gradually. Phalcon\Paginator offers a fast and convenient way to split these sets of data browsable pages.

数据适配器Data Adapters

该组件使用了适配器封装不同来源的数据:

This component makes use of adapters to encapsulate different sources of data:

AdapterDescription
NativeArrayUse a PHP array as source data
ModelUse a Phalcon\Mvc\Model\Resultset object as source data. Since PDO doesn’t support scrollable cursors this adapter shouldn’t be used to paginate a large number of records
QueryBuilderUse a Phalcon\Mvc\Model\Query\Builder object as source data

示例Examples

在下面的示例中,paginator将使用从一个模型查询的结果作为源数据,并限制每页显示的数据到10记录:

In the example below, the paginator will use as its source data the result of a query from a model, and limit the displayed data to 10 records per page:

  1. <?php
  2. use Phalcon\Paginator\Adapter\Model as PaginatorModel;
  3. // Current page to show
  4. // In a controller this can be:
  5. // $this->request->getQuery('page', 'int'); // GET
  6. // $this->request->getPost('page', 'int'); // POST
  7. $currentPage = (int) $_GET["page"];
  8. // The data set to paginate
  9. $robots = Robots::find();
  10. // Create a Model paginator, show 10 rows by page starting from $currentPage
  11. $paginator = new PaginatorModel(
  12. array(
  13. "data" => $robots,
  14. "limit" => 10,
  15. "page" => $currentPage
  16. )
  17. );
  18. // Get the paginated results
  19. $page = $paginator->getPaginate();

变量$currentPage控件将显示在页面。$paginator->getPaginate()返回一个包含分页数据$page对象。它可以用于产生分页:

Variable $currentPage controls the page to be displayed. The $paginator->getPaginate() returns a $page object that contains the paginated data. It can be used for generating the pagination:

  1. <table>
  2. <tr>
  3. <th>Id</th>
  4. <th>Name</th>
  5. <th>Type</th>
  6. </tr>
  7. <?php foreach ($page->items as $item) { ?>
  8. <tr>
  9. <td><?php echo $item->id; ?></td>
  10. <td><?php echo $item->name; ?></td>
  11. <td><?php echo $item->type; ?></td>
  12. </tr>
  13. <?php } ?>
  14. </table>

$page对象包含导航链接数据:

The $page object also contains navigation data:

  1. <a href="/robots/search">First</a>
  2. <a href="/robots/search?page=<?= $page->before; ?>">Previous</a>
  3. <a href="/robots/search?page=<?= $page->next; ?>">Next</a>
  4. <a href="/robots/search?page=<?= $page->last; ?>">Last</a>
  5. <?php echo "You are in page ", $page->current, " of ", $page->total_pages; ?>

适配器使用Adapters Usage

为每个适配器设置数据源的例子如下:

An example of the source data that must be used for each adapter:

  1. <?php
  2. use Phalcon\Paginator\Adapter\Model as PaginatorModel;
  3. use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray;
  4. use Phalcon\Paginator\Adapter\QueryBuilder as PaginatorQueryBuilder;
  5. //Passing a resultset as data
  6. $paginator = new PaginatorModel(
  7. array(
  8. "data" => Products::find(),
  9. "limit" => 10,
  10. "page" => $currentPage
  11. )
  12. );
  13. //Passing an array as data
  14. $paginator = new PaginatorArray(
  15. array(
  16. "data" => array(
  17. array('id' => 1, 'name' => 'Artichoke'),
  18. array('id' => 2, 'name' => 'Carrots'),
  19. array('id' => 3, 'name' => 'Beet'),
  20. array('id' => 4, 'name' => 'Lettuce'),
  21. array('id' => 5, 'name' => '')
  22. ),
  23. "limit" => 2,
  24. "page" => $currentPage
  25. )
  26. );
  27. //Passing a querybuilder as data
  28. $builder = $this->modelsManager->createBuilder()
  29. ->columns('id, name')
  30. ->from('Robots')
  31. ->orderBy('name');
  32. $paginator = new PaginatorQueryBuilder(array(
  33. "builder" => $builder,
  34. "limit" => 20,
  35. "page" => 1
  36. ));

页面属性Page Attributes

$page对象包含如下属性:

The $page object has the following attributes:

AttributeDescription
itemsThe set of records to be displayed at the current page
currentThe current page
beforeThe previous page to the current one
nextThe next page to the current one
lastThe last page in the set of records
total_pagesThe number of pages
total_itemsThe number of items in the source data

自定义适配器Implementing your own adapters

Phalcon\Paginator\AdapterInterface 必须被实现如果要创建自己的分页适配器:

The Phalcon\Paginator\AdapterInterface interface must be implemented in order to create your own paginator adapters or extend the existing ones:

  1. <?php
  2. use Phalcon\Paginator\AdapterInterface as PaginatorInterface;
  3. class MyPaginator implements PaginatorInterface
  4. {
  5. /**
  6. * Adapter constructor
  7. *
  8. * @param array $config
  9. */
  10. public function __construct($config);
  11. /**
  12. * Set the current page number
  13. *
  14. * @param int $page
  15. */
  16. public function setCurrentPage($page);
  17. /**
  18. * Returns a slice of the resultset to show in the pagination
  19. *
  20. * @return stdClass
  21. */
  22. public function getPaginate();
  23. }