ChoiceFilter.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Filter\ORM;
  11. use Doctrine\ORM\QueryBuilder;
  12. class ChoiceFilter extends Filter
  13. {
  14. /**
  15. * @param QueryBuilder $queryBuilder
  16. * @param string $alias
  17. * @param string $field
  18. * @param mixed $value
  19. * @return
  20. */
  21. public function filter($queryBuilder, $alias, $field, $value)
  22. {
  23. if ($this->getField()->getAttribute('multiple')) {
  24. if (!is_array($value) || count($value) == 0) {
  25. return;
  26. }
  27. if (in_array('all', $value)) {
  28. return;
  29. }
  30. $queryBuilder->andWhere($queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field ), $value));
  31. } else {
  32. if (empty($value) || $value == 'all') {
  33. return;
  34. }
  35. $queryBuilder->andWhere(sprintf('%s.%s = :%s', $alias, $field, $this->getName()));
  36. $queryBuilder->setParameter($this->getName(), $value);
  37. }
  38. }
  39. }