Filter the values ​​of the model SonataAdminBundle model_type in the form of a filter

I have such a field

    /**
     * @ORM\ManyToOne(targetEntity="Town")
    **/
    protected $town;

and admin class with this method

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
        ...
        ->add('town', null, array('label' => 'Town'), null, array('expanded' => true, 'multiple' => true))
        ;
    }

He gives me a filter like this:

symfony2 SonataAdminBundle fulter example with doctrine entity And my question is: can I install custom sql \ dql to search for City ? For example, select only cities with the identifier IN (1, 2)?

+5
source share
1 answer

OK I understood. Here is the solution:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
    ...
    ->add('town', null, array('label' => 'Town'), null, array(
        'expanded' => true,
        'multiple' => true,
        'query_builder' => function (\Doctrine\ORM\EntityRepository $repository) {
            return $repository->createQueryBuilder('t')
                    ->where('t.id = ?1')
                    ->setParameter(1, 1)
                    ->add('orderBy', 't.name ASC');
        }
    ))
    ...
    ;
}
+8
source

All Articles