StringFilter.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 Symfony\Component\Form\FormFactory;
  12. use Doctrine\ORM\QueryBuilder;
  13. class StringFilter extends Filter
  14. {
  15. public function filter($queryBuilder, $alias, $field, $value)
  16. {
  17. if ($value == null) {
  18. return;
  19. }
  20. $value = sprintf($this->getOption('format'), $value);
  21. // c.name LIKE '%word%' => c.name LIKE :fieldName
  22. $queryBuilder->andWhere(sprintf('%s.%s LIKE :%s',
  23. $alias,
  24. $field,
  25. $this->getName()
  26. ));
  27. $queryBuilder->setParameter($this->getName(), $value);
  28. }
  29. public function getDefaultOptions()
  30. {
  31. return array(
  32. 'format' => '%%%s%%'
  33. );
  34. }
  35. public function defineFieldBuilder(FormFactory $formFactory)
  36. {
  37. $options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
  38. $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
  39. }
  40. }