IntegerFilter.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 IntegerFilter 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 > '1' => c.name OPERATOR :FIELDNAME
  22. $queryBuilder->andWhere(sprintf('%s.%s %s :%s',
  23. $alias,
  24. $field,
  25. $this->getOption('operator'),
  26. $this->getName()
  27. ));
  28. $queryBuilder->setParameter($this->getName(), $value);
  29. }
  30. protected function configure()
  31. {
  32. $this->addOption('operator', '=');
  33. $this->addOption('format', '%d');
  34. parent::configure();
  35. }
  36. public function getFormField()
  37. {
  38. return new \Symfony\Component\Form\TextField(
  39. $this->getName(),
  40. $this->description->getOption('filter_field_options', array())
  41. );
  42. }
  43. }