Last active
August 5, 2018 03:28
-
-
Save webdevilopers/c04abcff801c7837d25f to your computer and use it in GitHub Desktop.
(NOT) NULL doctrine_orm_callback filter with Sonata Admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sonata\AdminBundle\Datagrid\DatagridMapper; | |
use Sonata\CoreBundle\Form\Type\BooleanType; | |
class PersonAdmin extends Admin | |
{ | |
protected function configureDatagridFilters(DatagridMapper $filterMapper) | |
{ | |
$filterMapper | |
->add('user', 'doctrine_orm_callback', array( | |
'label' => 'Login', | |
'translation_domain' => 'SonataCoreBundle', | |
'callback' => function($queryBuilder, $alias, $field, $value) { | |
if (!isset($value['value'])) { | |
return; | |
} | |
if ($value['value'] == BooleanType::TYPE_YES) { | |
$queryBuilder->andWhere(sprintf('$s.$s IS NOT NULL', $alias, $field)); | |
} | |
if ($value['value'] == BooleanType::TYPE_NO) { | |
$queryBuilder->andWhere(sprintf('$s.$s IS NULL', $alias, $field)); | |
} | |
return true; | |
} | |
), 'choice', array( | |
'translation_domain' => 'SonataCoreBundle', | |
'choices' => array( | |
BooleanType::TYPE_YES => 'label_type_yes', | |
BooleanType::TYPE_NO => 'label_type_no' | |
) | |
) | |
) | |
; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why
$s
in$queryBuilder->andWhere(sprintf('$s.$s IS NOT NULL', $alias, $field));
instead%s
?