Search code examples
symfonysonata-adminsonata

Specify query for datagridfilter sonata


When using the doctrine_orm_model type for the datagridfilter. Is there a way to define a custom query to fetch the possible filter values?

Lets say I do not want to load all the entities for that specific type. I can not find any way to specify a query or something.


Solution

  • Yes you can like this your admin class. Important is 5 parameter, with query builder.

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('modelField', null, [], 'entity', [
                'class' => 'App\Entity\YourModel',
                'choice_label' => 'name', // or something else as label
                'query_builder' => function(YourModelRepository $repository) {
                    // return your query builder
                }
            ]
        );     
    }