phpzend-frameworkdoctrine-ormpaginationzend-paginator

doctrine 2 and zend paginator


i want to use doctrine with zend_paginator

here some example query :

$allArticleObj = $this->_em->getRepository('Articles'); $qb = $this->_em->createQueryBuilder();

    $qb->add('select', 'a')
            ->add('from', 'Articles a')
            ->setFirstResult(0)
            ->setMaxResults(5);

is there any example code to show we can write a zend_paginator adapter for doctrine 2 query builder?


Solution

  • The upshot, of course, is that you have to implement the Zend_Paginator_Adapter_Interface, which essentially means implementing the two methods:

    count()

    getItems($offset, $perPage)

    Your adapter would accept the Doctrine query as a constructor argument.

    In principle, the getItems() part is actually straightforward. Simply, add the $offset and $perPage restrictions to the query - as you are doing in your sample - and execute the query.

    In practice, it's the count() business that tends to be tricky. I'd follow the example of Zend_Paginator_Adapter_DbSelect, replacing the Zend_Db operations with their Doctrine analogues.