Skip to content

Instantly share code, notes, and snippets.

@joergbasedow
Last active August 5, 2018 03:28
Show Gist options
  • Save joergbasedow/5964815 to your computer and use it in GitHub Desktop.
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?
<?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'
),
));
}
// ...
}
@webdevilopers
Copy link

You should also return true if a value is filtered. Otherwise the active filter will not be shown inside the filters:
sonata-project/SonataAdminBundle#2513 (comment)

@webdevilopers
Copy link

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