sonata-project/SonataDoctrineORMAdminBundle — Issue #5: Filter Field: doctrine_orm_callback
Author: sebastianblum
Created: 2011-11-07 15:21:09+00:00
State: open

Dear Sonata Team,

I'am working with Admin Bundle and created own Filter Fields using doctrine_orm_callback.

If I follow the instructions on http://sonata-project.org/bundles/admin/master/doc/reference/filter_field_definition.html,
the Filter will be executed every time with the checkbox is checked.

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('with_open_comments', 'doctrine_orm_callback', array(
'callback' => array($this, 'getWithOpenCommentFilter'),
'field_type' => 'checkbox'
))
;
}

public function getWithOpenCommentFilter($queryBuilder, $alias, $field, $value)
{

// FOR DEBUGGING
print_r($value);

if (!$value) {
return;
}

$queryBuilder->leftJoin(sprintf('%s.comments', $alias), 'c');
$queryBuilder->andWhere('c.status = :status');
$queryBuilder->setParameter('status', Comment::STATUS_MODERATE);
}

------------

If you debug the value of $value, you get the following Output:

not checked checkbox:
Array ( [type] => )

checked checkbox:
Array ( [type] => [value] => 1 )

----------------

So
if (!$value) {
return;
}
does not work and the Filter will always been active.

In my opion $value should be null the checkbox is not checked.