分页(Pagination)

当需要呈现大量数据时我们需要用到该分页组件。Phalcon\Paginator 提供了便捷数据集访问接口。

数据适配器(Data Adapters)

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

Adapter Description
NativeArray 使用数组当数据源。
Model 使用模型结果集 Phalcon\Mvc\Model\Resultset <../api/Phalcon_Mvc_Model_Resultset> 当数据源。因为 PDO 不支持游标,所以不适合大数据量的翻页。
QueryBuilder Use a Phalcon\Mvc\Model\Query\Builder object as source data
Sql 通过设置原生 SQL 获取数据源。

示例(Examples)

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

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

The $currentPage variable controls the page to be displayed. The $paginator->getPaginate() returns a $pageobject 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>

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.  
  6. <?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.  
  3. use Phalcon\Paginator\Adapter\Model as PaginatorModel;
  4. use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray;
  5. use Phalcon\Paginator\Adapter\QueryBuilder as PaginatorQueryBuilder;
  6. use Phalcon\Paginator\Adapter\Sql as PaginatorSql;
  7.  
  8. // Passing a resultset as data
  9. $paginator = new PaginatorModel(
  10. array(
  11. "data" => Products::find(),
  12. "limit" => 10,
  13. "page" => $currentPage
  14. )
  15. );
  16.  
  17. // Passing an array as data
  18. $paginator = new PaginatorArray(
  19. array(
  20. "data" => array(
  21. array('id' => 1, 'name' => 'Artichoke'),
  22. array('id' => 2, 'name' => 'Carrots'),
  23. array('id' => 3, 'name' => 'Beet'),
  24. array('id' => 4, 'name' => 'Lettuce'),
  25. array('id' => 5, 'name' => '')
  26. ),
  27. "limit" => 2,
  28. "page" => $currentPage
  29. )
  30. );
  31.  
  32. // Passing a QueryBuilder as data
  33.  
  34. $builder = $this->modelsManager->createBuilder()
  35. ->columns('id, name')
  36. ->from('Robots')
  37. ->orderBy('name');
  38.  
  39. $paginator = new PaginatorQueryBuilder(
  40. array(
  41. "builder" => $builder,
  42. "limit" => 20,
  43. "page" => 1
  44. )
  45. );
  46.  
  47. $paginator = new PaginatorSql(
  48. array(
  49. "sql" => "SELECT * FROM robots WHERE type = :type LIMIT :limit OFFSET :offset",
  50. "total_sql" => "SELECT COUNT(*) rowcount FROM robots WHERE type = :type",
  51. "bind" => ['type' => 'google'],
  52. "limit" => 20,
  53. "page" => 1
  54. )
  55. );

页面属性(Page Attributes)

The $page object has the following attributes:

Attribute Description
items The set of records to be displayed at the current page
current The current page
before The previous page to the current one
next The next page to the current one
last The last page in the set of records
total_pages The number of pages
total_items The number of items in the source data

自定义适配器(Implementing your own adapters)

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

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

原文: http://www.myleftstudio.com/reference/pagination.html