IntegerFilter.php 1.2 KB

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