With SonataAdminBundle. Set up a filter on a two-stage linked object

I would like to know what the weather is possible and how to set up a filter to present the list as follows: SonataAdminBundle in Symfony 2

Say I have entities. Order indicating entities User pointing to companies. I want to configure filters for both user filtering and filtering by a company (user company) The first one is straightforward. Secondly, I am trying to clean.

In the OrderAdmin class, I would rewrite configureDatagridFilters as:

protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('created_at') //... some other filters on Order fields, as usual // the filter on User, provided 'user', no ploblem ->add('user') // and the filter by Company ->add('user.company') // this doesn't work, of course ; } 

This syntax for the company filter is used by sonta docs: http://sonata-project.org/bundles/doctrine-orm-admin/2-0/doc/reference/filter_field_definition.html

Not intended for what I'm trying to learn, but can't find what to look at.

Hope someone has the key to this.

thanks

+6
source share
2 answers

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; } 
+15
source

All Articles