Doctrine + ZF2 tips: Pagination

Doctrine’s ORM Module offers a very simple and elegant way to integrate the Object Relational Mapper (ORM) into Zend Framework 2 (ZF2) . There are a few really handy classes shifted with this module, allowing Doctrine to be nicely integrated with ZF2 components. Here are a few useful components like paginator.

Doctrine has really good support for Zend pagination. You can use the default view helper (paginationControl); all you need to do is to set the page number, and pass the paginator in the view:

use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use Zend\Paginator\Paginator;
use Zend\View\Model\ViewModel;
// .......
$view =  new ViewModel();

$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$repository = $entityManager->getRepository('Admin\Entity\SystemUser');
$adapter = new DoctrineAdapter(new ORMPaginator($repository->createQueryBuilder('user')));
$paginator = new Paginator($adapter);

$page = (int)$this->params()->fromQuery('page');
if($page) $paginator->setCurrentPageNumber($page);


Latest News & Insights

Say connected – get Loft updates straight to your inbox.