EasyAdminExtensionBundle icon indicating copy to clipboard operation
EasyAdminExtensionBundle copied to clipboard

EmbeddedList, on empty many-to-many attributes, ignore filters

Open McLone opened this issue 5 years ago • 4 comments

Hi!

It looks like the url generated for the subquery drops the "filters" attribute on Generator/UrlGenerator.php:275 in symfony/routing v4.2.10 (but I guess it should impact all versions) :

if ($extra && $query = http_build_query($extra, '', '&', PHP_QUERY_RFC3986)) {

$extra does have a key 'filters' with an array 'entity.id' => [], but http_build_query seems to not like empty array and does'nt put filters in its query (see https://stackoverflow.com/questions/2930592/http-build-query-ignores-the-key-if-the-value-is-an-empty-array-how-is-this-not).

A quick and dirty fix would be to replace, in src/Helper/EmbeddedListHelper.php:144 (v2.1.6) :

return ['entity.id' => $itemIds->toArray()];

by

$array = $itemIds->toArray();
if(empty($array)) {
    $array = [0];
}
return ['entity.id' => $array];

... but only works if we're sure to never have entities with "0" id.

Thanks to all contributor of this bundle 👍

McLone avatar Jul 25 '19 08:07 McLone

Hi @McLone !

Empty arrays are ignored by dynamic filtered queries. This is a known limitation, and I prefer not to try to work around this.

Which case are trying to solve. Can't you test if your value list is not empty before generating the URL ?

alterphp avatar Jul 25 '19 09:07 alterphp

@alterphp I'm not sure a test would apply on my case :

I have the following configuration:

entities:
    User:
        class: App\Entity\User
        show:
            fields:
                - email
                - { property: 'cars', label: 'Cars', type: embedded_list }

with cars having a many-to-many relation.

When I'm in the "show" of a user with no car assigned, I expect to see an "empty result", but I see all cars of my database.

McLone avatar Jul 25 '19 09:07 McLone

@McLone thanks for this, I just decorated this service alterphp.easyadmin_extension.helper.embedded_list with your workaround.

botjaeger avatar Sep 06 '19 02:09 botjaeger

I guess I am experiencing the same problem here. I also have a many-to-many relation.

easy_admin:
    entities:
        Assessment:
            class: App\Entity\Assessment
            form:
                fields:
                    - externalReference
                    - seniorityLevel
                    - tags
                    - { property: questions, type: embedded_list }

It shows all questions at all times. My expectation was that it actually only shows the ones actually related to Assessment.

Chrisissorry avatar Oct 02 '19 09:10 Chrisissorry