Pagination

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 into browsable pages.

Data Adapters

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

AdapterDescription
Phalcon\Paginator\Adapter\NativeArrayUse a PHP array as source data
Phalcon\Paginator\Adapter\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
Phalcon\Paginator\Adapter\QueryBuilderUse a Phalcon\Mvc\Model\Query\Builder object as source data

Factory

Loads Paginator Adapter class using adapter option

  1. <?php
  2. use Phalcon\Paginator\Factory;
  3. $builder = $this->modelsManager->createBuilder()
  4. ->columns('id, name')
  5. ->from('Robots')
  6. ->orderBy('name');
  7. $options = [
  8. 'builder' => $builder,
  9. 'limit' => 20,
  10. 'page' => 1,
  11. 'adapter' => 'queryBuilder',
  12. ];
  13. $paginator = Factory::load($options);

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. use Phalcon\Paginator\Adapter\Model as PaginatorModel;
  3. // Current page to show
  4. // In a controller/component 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. [
  13. 'data' => $robots,
  14. 'limit' => 10,
  15. 'page' => $currentPage,
  16. ]
  17. );
  18. // Get the paginated results
  19. $page = $paginator->getPaginate();

The $currentPage variable 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>

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; ?>

Using Adapters

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. [
  8. 'data' => Products::find(),
  9. 'limit' => 10,
  10. 'page' => $currentPage,
  11. ]
  12. );
  13. // Passing an array as data
  14. $paginator = new PaginatorArray(
  15. [
  16. 'data' => [
  17. ['id' => 1, 'name' => 'Artichoke'],
  18. ['id' => 2, 'name' => 'Carrots'],
  19. ['id' => 3, 'name' => 'Beet'],
  20. ['id' => 4, 'name' => 'Lettuce'],
  21. ['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(
  33. [
  34. 'builder' => $builder,
  35. 'limit' => 20,
  36. 'page' => 1,
  37. ]
  38. );

Page Attributes

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

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. }