StringFilter.php 1.1 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 Bundle\Sonata\BaseApplicationBundle\Filter;
  11. class StringFilter extends Filter
  12. {
  13. public function filter($query_builder, $alias, $field, $value)
  14. {
  15. if($value == null) {
  16. return;
  17. }
  18. $value = sprintf($this->getOption('format'), $value);
  19. // c.name LIKE '%word%' => c.name LIKE :fieldName
  20. $query_builder->andWhere(sprintf('%s.%s LIKE :%s',
  21. $alias,
  22. $field,
  23. $this->getName()
  24. ));
  25. $query_builder->setParameter($this->getName(), $value);
  26. }
  27. protected function configure()
  28. {
  29. $this->addOption('format', '%%%s%%');
  30. parent::configure();
  31. }
  32. public function getFormField()
  33. {
  34. return new \Symfony\Component\Form\TextField(
  35. $this->getName(),
  36. $this->description['filter_field_options']
  37. );
  38. }
  39. }