Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonataAdminBundle model_type values filter in filter form

I have such field

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

and Admin class with such method

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

It gives me such filter:

symfony2 SonataAdminBundle fulter example with doctrine entity And my question is: can i set custom sql\dql for Town entity retrieving? For example, select only Towns with id IN (1, 2)?

like image 472
striker Avatar asked Aug 25 '12 02:08

striker


1 Answers

Ok, i got it. Here is a 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');
        }
    ))
    ...
    ;
}
like image 188
striker Avatar answered Oct 20 '22 16:10

striker