How to use Zend_Paginator

My friend wrote in the sandbox article, which appeared to me (and not only) useful, but after 7 days was removed. With his permission I quote it below. If to whom it will also be useful, he asks to send him the invite alxsad@gmail.com .

Hello to all lovers of the Zend Framework. I want to tell how to use the Zend_Paginator. I often saw him badly some programmers. Let's look at the code below:

$pages = new Model_Pages();
$paginator = Zend_Paginator::factory($pages->getRows());
$paginator- > setItemCountPerPage(1);
$paginator->setPageRange(1);
$paginator- > setCurrentPageNumber($this->getRequest()->getParam('page', 1));
Zend_Paginator::setDefaultScrollingStyle('Sliding');
$this->view->pages = $paginator;
$paginator- > setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('paginator.phtml');
$this->view->paginator = $paginator;


* This source code was highlighted with Source Code Highlighter.


The code I have met on so many blogs, and even if I'm not mistaken, in the manual for Zend Framework. Now let's look at the request that we get the result:

image

See? The problem is that people just take ALL the records from the database, and then choose the needed. This is a huge mistake. So read how it's done
right.

We add to the table model, this method:

public function getPaginatorRows ($pageNumber = 1)
{
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($this->select()));
$paginator- > setCurrentPageNumber($pageNumber);
$paginator- > setItemCountPerPage(1);
$paginator->setPageRange(1);
return $paginator;
}

* This source code was highlighted with Source Code Highlighter.


and then in the controller call it:

$pages = new Model_Pages();
$this->view->pages = $pages->getPaginatorRows((int) $this->getRequest()->getParam('page', 1));


* This source code was highlighted with Source Code Highlighter.


Voila!

image
Article based on information from habrahabr.ru

Комментарии