Finally, I found the answer, guided by this other question: How to create a custom DataGrid filter in SonataAdmin and more detailed familiarization with the link to the sonata documents that I inserted? in my question.
In case someone has this problem and using the previous example:
protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper //... whatever filter // and the filter by Company ->add('company', 'doctrine_orm_callback', array( 'callback' => array($this, 'callbackFilterCompany'), 'field_type' => 'checkbox' ), 'choice', array('choices' => $this -> getCompanyChoices()) ; }
where the getCompanyChoices method retrieves an associative array of ids => company identifiers (for example). And the callbackFilterCompany method is as follows
public function callbackFilterCompany ($queryBuilder, $alias, $field, $value) { if(!is_array($value) or !array_key_exists('value', $value) or empty($value['value'])){ return; } $queryBuilder ->leftJoin(sprintf('%s.user', $alias), 'u') ->leftJoin('u.company', 'c') ->andWhere('c.id = :id') ->setParameter('id', $value['value']) ; return true; }
source share