📜 ⬆️ ⬇️

How to use Zend_Paginator correctly

My friend wrote an article in the sandbox, which was useful for me (and not only), but was deleted after 7 days. With his permission, I quote it below. If she is also useful to someone, then he asks to send him an invite to alxsad@gmail.com.

Hello to all fans of the Zend Framework. I want to tell you how to correctly use the Zend_Paginator component. I have often seen how poorly some programmers work with it. 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 .

')
I have met this code on a great many blogs, and even, if I am not mistaken, in the manual on the Zend Framework. Let's now look at the query that we get as a result of:

image

See you The problem is that people immediately take ALL the records from the database, and then select the necessary ones from them. This is a huge mistake. Therefore, we read how to do it.
right.

We add this method to the table model:

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 we 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

Source: https://habr.com/ru/post/72399/


All Articles