doctrine 2 and zend paginator

前端 未结 4 918
闹比i
闹比i 2021-01-01 01:19

i want to use doctrine with zend_paginator

here some example query :

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 01:50

    I was very surprised how hard it was to find an adapter example that doesn't cause performance issues with large collections for Doctrine 2 and ZF1.

    My soultion does use the the Doctrine\ORM\Tools\Pagination\Paginator from Doctrine 2.2 just like @Kurt's answer; however the difference that this will return the doctrine paginator (Which is it's self an \Iterator) from getItems without the entire query results being hydrated.

    use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
    use Doctrine\ORM\Query;
    
    class DoctrinePaginatorAdapter implements \Zend_Paginator_Adapter_Interface
    {
        protected $query;
    
        public function __construct(Query $query)
        {
            $this->query = $query;
        }
    
        public function count()
        {
            return $this->createDoctrinePaginator($this->query)->count();
        }
    
        public function getItems($offset, $itemCountPerPage)
        {
            $this->query->setFirstResult($offset)
                        ->setMaxResults($itemCountPerPage);
    
            return $this->createDoctrinePaginator($this->query);
        }
    
        protected function createDoctrinePaginator(Query $query, $isFetchJoinQuery = true)
        {
            return new DoctrinePaginator($query, $isFetchJoinQuery);
        }
    
    } 
    

提交回复
热议问题