StringFilter.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 || strlen($value) == 0) {
  23. return;
  24. }
  25. // c.name LIKE '%word%' => c.name LIKE :fieldName
  26. $queryBuilder->andWhere(sprintf('%s.%s LIKE :%s', $alias, $field, $this->getName()));
  27. $queryBuilder->setParameter($this->getName(), sprintf($this->getOption('format'), $value));
  28. }
  29. /**
  30. * @return array
  31. */
  32. public function getDefaultOptions()
  33. {
  34. return array(
  35. 'format' => '%%%s%%'
  36. );
  37. }
  38. }