IntegerFilter.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. use Doctrine\ORM\QueryBuilder;
  12. class IntegerFilter extends Filter
  13. {
  14. public function filter($queryBuilder, $alias, $field, $value)
  15. {
  16. if ($value == null) {
  17. return;
  18. }
  19. $value = sprintf($this->getOption('format'), $value);
  20. // c.name > '1' => c.name OPERATOR :FIELDNAME
  21. $queryBuilder->andWhere(sprintf('%s.%s %s :%s',
  22. $alias,
  23. $field,
  24. $this->getOption('operator'),
  25. $this->getName()
  26. ));
  27. $queryBuilder->setParameter($this->getName(), (int)$value);
  28. }
  29. protected function configure()
  30. {
  31. $this->addOption('operator', '=');
  32. $this->addOption('format', '%d');
  33. parent::configure();
  34. }
  35. public function getFormField()
  36. {
  37. return new \Symfony\Component\Form\TextField(
  38. $this->getName(),
  39. $this->description->getOption('filter_field_options', array('required' => false))
  40. );
  41. }
  42. }