Filter.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\DoctrineORMAdminBundle\Filter;
  11. use Sonata\AdminBundle\Filter\Filter as BaseFilter;
  12. abstract class Filter extends BaseFilter
  13. {
  14. protected $active = false;
  15. protected $parameterUniqueId = 0;
  16. public function apply($queryBuilder, $value)
  17. {
  18. $this->value = $value;
  19. list($alias, $field) = $this->association($queryBuilder, $value);
  20. $this->filter($queryBuilder, $alias, $field, $value);
  21. }
  22. protected function association($queryBuilder, $value)
  23. {
  24. return array($this->getOption('alias', $queryBuilder->getRootAlias()), $this->getFieldName());
  25. }
  26. protected function applyWhere($queryBuilder, $parameter)
  27. {
  28. if ($this->getCondition() == self::CONDITION_OR) {
  29. $queryBuilder->orWhere($parameter);
  30. } else {
  31. $queryBuilder->andWhere($parameter);
  32. }
  33. // filter is active since it's added to the queryBuilder
  34. $this->active = true;
  35. }
  36. protected function getNewParameterName()
  37. {
  38. // dots are not accepted in a DQL identifier so replace them
  39. // by underscores. To avoid any name conflict with two filters
  40. // named myEntity.myProperty and myEntity_myProperty used at the
  41. // same time, we also use the object hash, that are different
  42. // for two objects alive at the same time.
  43. return str_replace('.', '_', $this->getName()).'_'.spl_object_hash($this).'_'.($this->parameterUniqueId++);
  44. }
  45. public function isActive()
  46. {
  47. return $this->active;
  48. }
  49. }