Skip to content

Commit 1a46362

Browse files
authored
fix(doctrine): group or filter in an AndWhere #7441 (#7445)
1 parent f3c811d commit 1a46362

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Doctrine/Orm/Filter/FreeTextQueryFilter.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
2121
use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
2222
use ApiPlatform\Metadata\Operation;
23+
use Doctrine\Common\Collections\ArrayCollection;
2324
use Doctrine\ORM\QueryBuilder;
2425

2526
final class FreeTextQueryFilter implements FilterInterface, ManagerRegistryAwareInterface, LoggerAwareInterface
@@ -46,14 +47,26 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
4647
}
4748

4849
$parameter = $context['parameter'];
50+
$qb = clone $queryBuilder;
51+
$qb->resetDQLPart('where');
52+
$qb->setParameters(new ArrayCollection());
4953
foreach ($this->properties ?? $parameter->getProperties() ?? [] as $property) {
5054
$this->filter->apply(
51-
$queryBuilder,
55+
$qb,
5256
$queryNameGenerator,
5357
$resourceClass,
5458
$operation,
5559
['parameter' => $parameter->withProperty($property)] + $context
5660
);
5761
}
62+
63+
$queryBuilder->andWhere($qb->getDQLPart('where'));
64+
$parameters = $queryBuilder->getParameters();
65+
66+
foreach ($qb->getParameters() as $p) {
67+
$parameters->add($p);
68+
}
69+
70+
$queryBuilder->setParameters($parameters);
5871
}
5972
}

tests/Functional/Parameters/OrFilterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ChickenCoop;
2121
use ApiPlatform\Tests\RecreateSchemaTrait;
2222
use ApiPlatform\Tests\SetupClassResourcesTrait;
23+
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector;
2324
use Doctrine\ODM\MongoDB\MongoDBException;
2425
use PHPUnit\Framework\Attributes\DataProvider;
2526

@@ -62,6 +63,22 @@ public function testOrFilter(string $url, int $expectedCount): void
6263
$this->assertJsonContains(['totalItems' => $expectedCount]);
6364
}
6465

66+
public function testOrFilterWithAnd(): void
67+
{
68+
if ($this->isMongoDB()) {
69+
$this->markTestSkipped();
70+
}
71+
$client = self::createClient();
72+
$client->enableProfiler();
73+
$client->request('GET', '/chickens?autocomplete=978020137962&chickenCoop=/chicken_coops/2');
74+
$profile = $client->getProfile();
75+
$this->assertResponseIsSuccessful();
76+
77+
/** @var DoctrineDataCollector */
78+
$db = $profile->getCollector('db');
79+
$this->assertStringContainsString('WHERE c1_.id = ? AND (c2_.name = ? OR c2_.ean = ?))', end($db->getQueries()['default'])['sql']);
80+
}
81+
6582
public static function orFilterDataProvider(): \Generator
6683
{
6784
yield 'ean through autocomplete' => [

0 commit comments

Comments
 (0)