< 設定の読み込み


キャッシュによるパフォーマンス改善 >



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.


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

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:


// Current page to show
// In a controller 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 \Phalcon\Paginator\Adapter\Model(
        "data" => $robots,
        "limit"=> 10,
        "page" => $currentPage

// Get the paginated results
$page = $paginator->getPaginate();

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:

    <?php foreach ($page->items as $item) { ?>
        <td><?php echo $item->id; ?></td>
        <td><?php echo $item->name; ?></td>
        <td><?php echo $item->type; ?></td>
    <?php } ?>

The $page object also contains navigation data:

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


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


//Passing a resultset as data
$paginator = new \Phalcon\Paginator\Adapter\Model(
        "data"  => Products::find(),
        "limit" => 10,
        "page"  => $currentPage

//Passing an array as data
$paginator = new \Phalcon\Paginator\Adapter\NativeArray(
        "data"  => array(
            array('id' => 1, 'name' => 'Artichoke'),
            array('id' => 2, 'name' => 'Carrots'),
            array('id' => 3, 'name' => 'Beet'),
            array('id' => 4, 'name' => 'Lettuce'),
            array('id' => 5, 'name' => '')
        "limit" => 2,
        "page"  => $currentPage

//Passing a querybuilder as data

$builder = $this->modelsManager->createBuilder()
    ->columns('id, name')

$paginator = new Phalcon\Paginator\Adapter\QueryBuilder(array(
    "builder" => $builder,
    "limit"=> 20,
    "page" => 1


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


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


class MyPaginator implements Phalcon\Paginator\AdapterInterface

     * 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();