* (c) Jonathan H. Wage * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sonata\DoctrineORMAdminBundle\Datagrid; use Sonata\AdminBundle\Datagrid\Pager as BasePager; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; /** * Doctrine pager class. * * @author Jonathan H. Wage */ class Pager extends BasePager { protected $queryBuilder = null; /** * Returns a query for counting the total results. * * @return integer */ public function computeNbResult() { $countQuery = clone $this->getQuery(); if (count($this->getParameters()) > 0) { $countQuery->setParameters($this->getParameters()); } $countQuery->select(sprintf('count(DISTINCT %s.%s) as cnt', $countQuery->getRootAlias(), current($this->getCountColumn()))); return $countQuery->getSingleScalarResult(); } /** * Get all the results for the pager instance * * @param mixed $hydrationMode A hydration mode identifier * * @return array */ public function getResults($hydrationMode = Query::HYDRATE_OBJECT) { return $this->getQuery()->execute(array(), $hydrationMode); } /** * Get the query for the pager. * * @return \AdminBundle\Datagrid\ORM\ProxyQuery */ public function getQuery() { return $this->query; } public function init() { $this->resetIterator(); $this->setNbResults($this->computeNbResult()); $this->getQuery()->setFirstResult(null); $this->getQuery()->setMaxResults(null); if (count($this->getParameters()) > 0) { $this->getQuery()->setParameters($this->getParameters()); } if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) { $this->setLastPage(0); } else { $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage())); $this->getQuery()->setFirstResult($offset); $this->getQuery()->setMaxResults($this->getMaxPerPage()); } } }