StringFilter.php 1.1 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. class StringFilter extends Filter
  12. {
  13. /**
  14. * @param Querybuilder $queryBuilder
  15. * @param string $alias
  16. * @param string $field
  17. * @param mixed $value
  18. * @return
  19. */
  20. public function filter($queryBuilder, $alias, $field, $value)
  21. {
  22. if ($value == null) {
  23. return;
  24. }
  25. $value = sprintf($this->getOption('format'), $value);
  26. // c.name LIKE '%word%' => c.name LIKE :fieldName
  27. $queryBuilder->andWhere(sprintf('%s.%s LIKE :%s',
  28. $alias,
  29. $field,
  30. $this->getName()
  31. ));
  32. $queryBuilder->setParameter($this->getName(), $value);
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function getDefaultOptions()
  38. {
  39. return array(
  40. 'format' => '%%%s%%'
  41. );
  42. }
  43. }