Filter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 Sonata\AdminBundle\Admin\FieldDescription;
  12. use Sonata\AdminBundle\Filter\Filter as BaseFilter;
  13. use Symfony\Component\Form\Configurable;
  14. use Doctrine\ORM\QueryBuilder;
  15. abstract class Filter extends BaseFilter
  16. {
  17. public function apply($queryBuilder, $value)
  18. {
  19. $this->value = $value;
  20. $this->field->submit($value);
  21. list($alias, $field) = $this->association($queryBuilder, $this->field->getData());
  22. $this->filter($queryBuilder, $alias, $field, $this->field->getData());
  23. }
  24. protected function association($queryBuilder, $value)
  25. {
  26. if ($value) {
  27. if ($this->description->getType() == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
  28. $queryBuilder->leftJoin(
  29. sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->description->getFieldName()),
  30. $this->getName()
  31. );
  32. // todo : use the metadata information to find the correct column name
  33. return array($this->getName(), 'id');
  34. }
  35. }
  36. return array($queryBuilder->getRootAlias(), $this->description->getFieldName());
  37. }
  38. }