您必须使用 Paginator 类。我的一个存储库类有一个工作示例:
<?php
namespace Fluency\Bundle\GeneralBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository,
Doctrine\ORM\Tools\Pagination\Paginator;
use Fluency\Bundle\GeneralBundle\Entity\Country;
/**
* CountryRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CountryRepository extends EntityRepository
{
/**
* @param array $criteria
* @param null $limit
* @param null $start
* @return array with this format (0 => $result, 1 => $counter)
*/
public function findAllByCriteria($criteria = array(), $limit = null, $start = null)
{
$query = $this->getEntityManager()->createQueryBuilder()
->select('c')
->from($this->getEntityName(), 'c')
->orderBy('c.name', 'ASC');
if(null !== $limit)
{
$query->setMaxResults($limit);
}
if(null !== $start)
{
$query->setFirstResult($start);
}
if(!empty($criteria))
{
if($criteria['active'])
{
$query->andWhere($query->expr()->eq('c.active', $criteria['active']));
}
}
$paginator = new Paginator($query);
return array($query->getQuery()->getArrayResult(), $paginator->count());
}
.................
}