Pagination


    The process of pagination takes place when we need to present big groups of arbitrary data gradually. 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:

    You can use the Pagination Factory class to instantiate a new paginator object:

    1. <?php
    2. use Phalcon\Paginator\PaginatorFactory;
    3. $builder = $this
    4. ->modelsManager
    5. ->createBuilder()
    6. ->columns('id, lastName, firstName')
    7. ->from(Users::class)
    8. ->orderBy('name')
    9. ;
    10. $options = [
    11. 'builder' => $builder,
    12. 'limit' => 20,
    13. 'page' => 1,
    14. 'adapter' => 'queryBuilder',
    15. ];
    16. $paginator = (new PaginatorFactory())->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:

    The $currentPage variable controls the page to be displayed. The $paginator->paginate() 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>Active</th>
    5. <th>Last Name</th>
    6. <th>First Name</th>
    7. </tr>
    8. <tr>
    9. <td><?php echo $item->id; ?></td>
    10. <td><?php echo ($item->active) ? 'Y' : 'N'; ?></td>
    11. <td><?php echo $item->lastName; ?></td>
    12. <td><?php echo $item->firstName; ?></td>
    13. </tr>
    14. </table>

    The $page object also contains navigation data:

    1. <?php
    2. use Phalcon\Paginator\AdapterFactory;
    3. $factory = new AdapterFactory();
    4. // Passing a resultset as data
    5. $options = [
    6. 'data' => Products::find(),
    7. 'limit' => 10,
    8. 'page' => $currentPage,
    9. ];
    10. $paginator = $factory->newInstance('model', $options);
    11. // Passing an array as data
    12. $options = [
    13. 'data' => [
    14. ['id' => 1, 'name' => 'Artichoke'],
    15. ['id' => 2, 'name' => 'Carrots'],
    16. ['id' => 3, 'name' => 'Beet'],
    17. ['id' => 4, 'name' => 'Lettuce'],
    18. ['id' => 5, 'name' => ''],
    19. ],
    20. 'limit' => 2,
    21. 'page' => $currentPage,
    22. ];
    23. $paginator = $factory->newInstance('nativeArray', $options);
    24. // Passing a QueryBuilder as data
    25. ->modelsManager
    26. ->createBuilder()
    27. ->columns('id, name')
    28. ->from('Robots')
    29. $options = [
    30. 'builder' => $builder,
    31. 'limit' => 20,
    32. 'page' => 1,
    33. ];
    34. $paginator = $factory->newInstance('queryBuilder', $options);

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

    Page Attributes

    The $page object has the following attributes:

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

    The 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. use Phalcon\Paginator\RepositoryInterface;
    4. class MyPaginator implements PaginatorInterface
    5. {
    6. /**
    7. * Get current rows limit
    8. */
    9. public function getLimit(): int;
    10. /**
    11. * Returns a slice of the resultset to show in the pagination
    12. */
    13. public function paginate(): RepositoryInterface;
    14. /**
    15. * Set the current page number
    16. */
    17. public function setCurrentPage(int $page);
    18. /**
    19. * Set current rows limit
    20. */
    21. }