Filter.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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\Filter\Filter as BaseFilter;
  12. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  13. use Symfony\Component\Form\FormBuilder;
  14. abstract class Filter extends BaseFilter
  15. {
  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. if ($value && $this->getFieldDescription()->getType() == ClassMetadataInfo::MANY_TO_MANY) {
  25. $queryBuilder->leftJoin(
  26. sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->getFieldDescription()->getFieldName()),
  27. $this->getName()
  28. );
  29. // todo : use the metadata information to find the correct column name
  30. return array($this->getName(), 'id');
  31. }
  32. return array($queryBuilder->getRootAlias(), $this->getFieldDescription()->getFieldName());
  33. }
  34. }