From c521f8b38cbcad44590b5619fcf79347acdb2154 Mon Sep 17 00:00:00 2001 From: Wybren Koelmans Date: Tue, 1 Sep 2020 14:17:43 +0200 Subject: [PATCH 1/4] Do not add JOINs for Filters without a value This potentially improves performance significantly with queries that use many filters on nested properties. --- src/Bridge/Doctrine/Orm/Filter/SearchFilter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Bridge/Doctrine/Orm/Filter/SearchFilter.php b/src/Bridge/Doctrine/Orm/Filter/SearchFilter.php index ad26c178b54..0fda7e6470d 100644 --- a/src/Bridge/Doctrine/Orm/Filter/SearchFilter.php +++ b/src/Bridge/Doctrine/Orm/Filter/SearchFilter.php @@ -81,16 +81,16 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB $alias = $queryBuilder->getRootAliases()[0]; $field = $property; - $associations = []; - if ($this->isPropertyNested($property, $resourceClass)) { - [$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass); - } - $values = $this->normalizeValues((array) $value, $property); if (null === $values) { return; } + $associations = []; + if ($this->isPropertyNested($property, $resourceClass)) { + [$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass); + } + $metadata = $this->getNestedMetadata($resourceClass, $associations); if ($metadata->hasField($field)) { From 6df637e2af3744b29839d58d65988e87a58b0a80 Mon Sep 17 00:00:00 2001 From: Wybren Koelmans Date: Thu, 3 Sep 2020 15:48:32 +0200 Subject: [PATCH 2/4] Do not add LookUp for Filters without a value Also do not add lookups for MongoDB --- src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php b/src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php index 91cb23dc3cf..153dae64fa4 100644 --- a/src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php +++ b/src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php @@ -77,6 +77,11 @@ protected function filterProperty(string $property, $value, Builder $aggregation $matchField = $field = $property; + $values = $this->normalizeValues((array) $value, $property); + if (null === $values) { + return; + } + $associations = []; if ($this->isPropertyNested($property, $resourceClass)) { [$matchField, $field, $associations] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass); @@ -87,11 +92,6 @@ protected function filterProperty(string $property, $value, Builder $aggregation */ $metadata = $this->getNestedMetadata($resourceClass, $associations); - $values = $this->normalizeValues((array) $value, $property); - if (null === $values) { - return; - } - $caseSensitive = true; if ($metadata->hasField($field) && !$metadata->hasAssociation($field)) { From e360020c894d5774211ed6dbbf090679a6aa9bb7 Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Mon, 1 Mar 2021 18:05:37 +0100 Subject: [PATCH 3/4] Add PHPUnit tests --- .../Doctrine/Common/Filter/SearchFilterTestTrait.php | 8 ++++++++ .../Doctrine/MongoDbOdm/Filter/SearchFilterTest.php | 4 ++++ tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/tests/Bridge/Doctrine/Common/Filter/SearchFilterTestTrait.php b/tests/Bridge/Doctrine/Common/Filter/SearchFilterTestTrait.php index 5062ed163a8..6916e3b9645 100644 --- a/tests/Bridge/Doctrine/Common/Filter/SearchFilterTestTrait.php +++ b/tests/Bridge/Doctrine/Common/Filter/SearchFilterTestTrait.php @@ -466,6 +466,14 @@ private function provideApplyTestArguments(): array 'relatedDummy.symfony' => 'exact', ], ], + 'empty nested property' => [ + [ + 'relatedDummy.symfony' => null, + ], + [ + 'relatedDummy.symfony' => [], + ], + ], ]; } } diff --git a/tests/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilterTest.php b/tests/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilterTest.php index b090f818409..a378e6e7d69 100644 --- a/tests/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilterTest.php +++ b/tests/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilterTest.php @@ -686,6 +686,10 @@ public function provideApplyTestData(): array ], $filterFactory, ], + 'empty nested property' => [ + [], + $filterFactory, + ], ] ); } diff --git a/tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php b/tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php index c202e3c5475..8add7097424 100644 --- a/tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php +++ b/tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php @@ -559,6 +559,11 @@ public function provideApplyTestData(): array ], $filterFactory, ], + 'empty nested property' => [ + sprintf('SELECT %s FROM %s %1$s', $this->alias, Dummy::class), + [], + $filterFactory, + ], ] ); } From 0ecfd29dadfe33cea03ef1d5f42f49efdd053a48 Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Mon, 1 Mar 2021 18:07:45 +0100 Subject: [PATCH 4/4] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdaebd63f18..2000f7e915e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Mercure: Do not use data in options when deleting (#4056) * Doctrine: Support for foreign identifiers (#4042) * Doctrine: Support for binary UUID in search filter (#3774) +* Doctrine: Do not add join or lookup for search filter with empty value (#3703) * JSON Schema: Allow generating documentation when property and method start from "is" (property `isActive` and method `isActive`) (#4064) * OpenAPI: Fix missing 422 responses in the documentation (#4086) * OpenAPI: Fix error when schema is empty (#4051)