IntegerFilter.php 1.3 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. use Symfony\Component\Form\FormFactory;
  13. class IntegerFilter extends Filter
  14. {
  15. public function filter($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(), (int)$value);
  29. }
  30. public function getDefaultOptions()
  31. {
  32. return array(
  33. 'operator' => '=',
  34. 'format' => '%d'
  35. );
  36. }
  37. public function defineFieldBuilder(FormFactory $formFactory)
  38. {
  39. $options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
  40. $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
  41. }
  42. }