Skip to content

Fix filters on nested associations and embedded properties#7500

Open
Seb33300 wants to merge 1 commit intoEasyCorp:5.xfrom
Seb33300:filters-nested-asso
Open

Fix filters on nested associations and embedded properties#7500
Seb33300 wants to merge 1 commit intoEasyCorp:5.xfrom
Seb33300:filters-nested-asso

Conversation

@Seb33300
Copy link
Contributor

@Seb33300 Seb33300 commented Mar 12, 2026

Same as #7295 with a different approach.

Fixes #7452 and #6664

In resolveNestedAssociations() I have extracted the logic handling nested associations from search to make it reusable in filters.
It also properly handles doctrine embeddables.

It's now possible to use associations in filters using the dot syntax:

    public function configureFilters(Filters $filters): Filters
    {
        return $filters
            // author is an association
            ->add(TextFilter::new('author.fullName', 'Author name'))
            // address is an Embeddable
            ->add(TextFilter::new('author.address.country', 'Author country'));
    }

@Seb33300 Seb33300 force-pushed the filters-nested-asso branch from 18c6ca4 to 646cbcb Compare March 12, 2026 04:19
@Seb33300 Seb33300 marked this pull request as draft March 12, 2026 04:28
@Seb33300 Seb33300 force-pushed the filters-nested-asso branch 2 times, most recently from a009235 to 6a93dff Compare March 12, 2026 06:09
@Seb33300 Seb33300 marked this pull request as ready for review March 12, 2026 06:14
@Seb33300 Seb33300 marked this pull request as draft March 12, 2026 07:11
@Seb33300 Seb33300 force-pushed the filters-nested-asso branch from 6a93dff to 1b66e5e Compare March 12, 2026 07:29
@Seb33300
Copy link
Contributor Author

Just found an issue with the EntityFilter + nested associations, I am investigating.

@Seb33300 Seb33300 force-pushed the filters-nested-asso branch 3 times, most recently from 9fbf27a to 1a53387 Compare March 12, 2026 09:18
* property_name: string,
* }
*/
public function resolveNestedAssociations(?QueryBuilder $queryBuilder, EntityDto $rootEntityDto, string $propertyName, bool $mustEndWithAssociation = false): array
Copy link
Contributor Author

@Seb33300 Seb33300 Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While extracting that logic from search, I also modified:

  • $queryBuilder is nullable so the resolveNestedAssociations() method can be called to resolve the nested EntityDto & property name without applying the joins to the query (eg: EntityConfigurator use it to resolve the entity used to fetch choice list)
  • $mustEndWithAssociation is used to allow property ending on an association field (eg: post.author instead of post.author.fullName) as required by EntityFilter

@Seb33300 Seb33300 marked this pull request as ready for review March 12, 2026 09:33
@Seb33300
Copy link
Contributor Author

Issue on EntityFilter is now fixed

@Seb33300 Seb33300 force-pushed the filters-nested-asso branch 3 times, most recently from 4823239 to 4758cb5 Compare March 12, 2026 10:00
Comment on lines +242 to +247
// Fallback to support custom filters with unmapped property names
$resolvedProperty = [
'entity_dto' => $entityDto,
'entity_alias' => current($queryBuilder->getRootAliases()),
'property_name' => $originalPropertyName,
];
Copy link
Contributor Author

@Seb33300 Seb33300 Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this fallback, AuthorWithMinPostsFilter was not working in the EasyAdmin demo project.

Copy link
Contributor Author

@Seb33300 Seb33300 Mar 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note about this:

Instead of try/catch, it would be cleaner to check if the filter is unmapped.
This may be achieved by checking the related form option as suggested by EA documentation:
https://symfony.com/bundles/EasyAdminBundle/current/filters.html#unmapped-filters

However, it looks like previous code was working on unmapped fields even if the mapped option was not specified.
For example, the custom filter from EA demo is not defining the mapped option, and it still work:
https://github.com/EasyCorp/easyadmin-demo/blob/main/src/Admin/Filter/AuthorWithMinPostsFilter.php

And if we replace the try/catch by a check on the form type option, this may introduce a BC break on unmapped filter that does not specify the unmapped option, but this should also fix this bug: #6386

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Seb33300 Seb33300 force-pushed the filters-nested-asso branch 3 times, most recently from 5ddf42f to e8ad127 Compare March 12, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Filter] Unable to use EntityFilter on nested accociations

1 participant