* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sonata\UserBundle\Entity; use FOS\UserBundle\Entity\GroupManager as BaseGroupManager; use Sonata\DatagridBundle\Pager\Doctrine\Pager; use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery; use Sonata\UserBundle\Model\GroupManagerInterface; /** * @author Hugo Briand */ class GroupManager extends BaseGroupManager implements GroupManagerInterface { /** * {@inheritdoc} */ public function findGroupsBy(array $criteria = null, array $orderBy = null, $limit = null, $offset = null) { return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } /** * {@inheritdoc} */ public function getPager(array $criteria, $page, $limit = 10, array $sort = array()) { $query = $this->repository ->createQueryBuilder('g') ->select('g'); $fields = $this->objectManager->getClassMetadata($this->class)->getFieldNames(); foreach ($sort as $field => $direction) { if (!in_array($field, $fields)) { throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class)); } } if (count($sort) == 0) { $sort = array('name' => 'ASC'); } foreach ($sort as $field => $direction) { $query->orderBy(sprintf('g.%s', $field), strtoupper($direction)); } $parameters = array(); if (isset($criteria['enabled'])) { $query->andWhere('g.enabled = :enabled'); $parameters['enabled'] = $criteria['enabled']; } $query->setParameters($parameters); $pager = new Pager(); $pager->setMaxPerPage($limit); $pager->setQuery(new ProxyQuery($query)); $pager->setPage($page); $pager->init(); return $pager; } }