StringFilter.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\BaseApplicationBundle\Filter;
  11. use Sonata\BaseApplicationBundle\Admin\FieldDescription;
  12. use Doctrine\ORM\QueryBuilder;
  13. class StringFilter extends Filter
  14. {
  15. public function filter(QueryBuilder $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. protected function configure()
  30. {
  31. $this->addOption('format', '%%%s%%');
  32. parent::configure();
  33. }
  34. public function getFormField()
  35. {
  36. return new \Symfony\Component\Form\TextField(
  37. $this->getName(),
  38. $this->description->getOption('filter_field_options', array('required' => false))
  39. );
  40. }
  41. }