Last active
August 5, 2018 03:28
-
-
Save joergbasedow/5964815 to your computer and use it in GitHub Desktop.
I'm trying to filter entities by wether a fields value is NULL/NOT NULL/BOTH in SonataAdminBundle. Is there a more concise way?
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 | |
// ... | |
class ProductAdmin extends SonataAdmin | |
{ | |
// ... | |
protected function configureDatagridFilters(DatagridMapper $filter) | |
{ | |
$filter->add('isImported', 'doctrine_orm_callback', array( | |
'label' => 'Ist importiert', | |
'callback' => function($queryBuilder, $alias, $field, $value) { | |
if ($value['value'] == 'yes') { | |
$queryBuilder->andWhere($alias . '.remoteId IS NOT NUll'); | |
} | |
if ($value['value'] == 'no') { | |
$queryBuilder->andWhere($alias . '.remoteId IS NUll'); | |
} | |
} | |
), 'choice', array( | |
'choices' => array( | |
'' => '', | |
'yes' => 'Ja', | |
'no' => 'Nein' | |
), | |
)); | |
} | |
// ... | |
} |
Here is a full version using the SonataCoreBundle
type constants and boolean translation domain:
https://gist.github.com/webdevilopers/c04abcff801c7837d25f
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should also return
true
if avalue
is filtered. Otherwise the active filter will not be shown inside the filters:sonata-project/SonataAdminBundle#2513 (comment)