IntegerFilter.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // 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)sprintf($this->getOption('format'), $value));
  28. }
  29. public function getDefaultOptions()
  30. {
  31. return array(
  32. 'operator' => '=',
  33. 'format' => '%d'
  34. );
  35. }
  36. public function defineFieldBuilder(FormFactory $formFactory)
  37. {
  38. $options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
  39. $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
  40. }
  41. }