æ•°æ®åˆ†é¡µ =============== 当有一大组数æ®éœ€è¦å‘ˆçŽ°æ—¶ï¼Œæˆ‘ä»¬éœ€è¦ç”¨åˆ°æ•°æ®åˆ†é¡µã€‚Phalcon\\Paginator æä¾›äº†ä¸€ä¸ªå¿«æ·ï¼Œæ–¹ä¾¿çš„æ–¹æ³•对大组数æ®è¿›è¡Œåˆ†å‰²ï¼Œä»¥è¾¾åˆ°åˆ†é¡µæµè§ˆçš„æ•ˆæžœã€‚ Data Adapters ------------- 这个组件使用ä¸åŒçš„适é…器æ¥å°è£…ä¸åŒçš„æ•°æ®æºï¼š +--------------+-------------------------------------------------------+ | Adapter | Description | +==============+=======================================================+ | NativeArray | Use a PHP array as source data | +--------------+-------------------------------------------------------+ | Model | Use a Phalcon\\Model\\Resultset object as source data | +--------------+-------------------------------------------------------+ Using Paginators ---------------- 在下é¢çš„例åä¸ï¼Œpaginator将从modelä¸è¯»å–æ•°æ®ä½œä¸ºå…¶æ•°æ®æºï¼Œå¹¶é™åˆ¶æ¯é¡µæ˜¾ç¤º10æ¡è®°å½•: .. code-block:: php <?php // 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( array( "data" => $robots, "limit"=> 10, "page" => $currentPage ) ); // Get the paginated results $page = $paginator->getPaginate(); å˜é‡ $currentPage 控制将显示哪一页。 $paginator->getPaginate() 返回一个包å«åˆ†é¡µæ•°æ®çš„ $page 对象,它将用于生æˆåˆ†é¡µï¼š .. code-block:: html+php <table> <tr> <th>Id</th> <th>Name</th> <th>Type</th> </tr> <?php foreach($page->items as $item) { ?> <tr> <td><?php echo $item->id; ?></td> <td><?php echo $item->name; ?></td> <td><?php echo $item->type; ?></td> </tr> <?php } ?> </table> $page对象还包å«ä»¥ä¸‹æ•°æ®ï¼š .. code-block:: html+php <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; ?> Page 属性 --------------- $page对象包å«ä»¥ä¸‹ä¸€äº›å±žæ€§ï¼š +---------+--------------------------------------------------------+ | Adapter | Description | +=========+========================================================+ | items | The set of records to be displayed at 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 | +---------+--------------------------------------------------------+ 实现自定义的分页适é…器 ------------------------------ The :doc:`Phalcon\\Paginator\\AdapterInterface <../api/Phalcon_Paginator_AdapterInterface>` interface must be implemented in order to create your own paginator adapters or extend the existing ones: .. code-block:: php <?php 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(); }