12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- * (c) Jonathan H. Wage <jonwage@gmail.com>
- *
- * 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 <jonwage@gmail.com>
- */
- 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());
- }
- }
- }
|