Easyadminbundle: Filter AssociationField results

Created on 3 Jul 2020  路  6Comments  路  Source: EasyCorp/EasyAdminBundle

Short description of what this feature will allow to do:
Add conditions to customize which entities are available in the (autocomplete) results.

Example of how to use this feature
I think the nicest way would be something like the following:

yield AssociationField::new('relation')
    ->autocomplete()
    ->filterResults(function(QueryBuilder $queryBuilder) {
        // can add conditions here, maybe we can also provide the current EntityDto in scope here?
    });

The autocomplete action in AbstractCrudController would then just need to call this callback before returning the QueryBuilder - not sure how that would best work, but probably by somehow passing the field name in the request.

Most helpful comment

It can be done like this:

AssociationField::new('assignedTo')
                ->setRequired(true)
                // disable sort option because of this issue
                // https://github.com/EasyCorp/EasyAdminBundle/issues/3379
                ->setSortable(false)
                ->setFormTypeOptions([
                    'query_builder' => function (UserRepository $er) {
                        return $er->createQueryBuilder('u')
                            ->orderBy('u.lastName', 'ASC');
                    },
                ])
            ,

You can easily add an andWhere clause

All 6 comments

I think I just asked for this functionality in slack https://symfony-devs.slack.com/archives/C3AT66207/p1593814573067300

It can be done like this:

AssociationField::new('assignedTo')
                ->setRequired(true)
                // disable sort option because of this issue
                // https://github.com/EasyCorp/EasyAdminBundle/issues/3379
                ->setSortable(false)
                ->setFormTypeOptions([
                    'query_builder' => function (UserRepository $er) {
                        return $er->createQueryBuilder('u')
                            ->orderBy('u.lastName', 'ASC');
                    },
                ])
            ,

You can easily add an andWhere clause

That will only work if it's using the default EntityType though, and not for the CrudAutocompleteType, which is used when you call ->autocomplete() (or set autocomplete option) on the field. There should be one function that works for both cases 馃檪

Yes I agree but for now this is a workaround that works for me

I got a rough version of this working, by borrowing the getCrudControllerInstance function from AdminContextListener - because I need to call configureFields on the instance of the "original" controller somehow. Everything else is pretty basic.

@javiereguiluz What do you think about somehow exposing this method or something similar to AbstractCrudController? It would also help for #3352 maybe.

That is a nice proposition as regarding the Entity "owner" of the relation, i need to customize the way i filter the associated entities .
Is it a other way to do it right now ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haithem-rihane picture haithem-rihane  路  4Comments

ziobudda picture ziobudda  路  4Comments

tamert picture tamert  路  3Comments

BigMichi1 picture BigMichi1  路  3Comments

nickicool picture nickicool  路  4Comments