Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Doctrine/Orm/Filter/FreeTextQueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
use ApiPlatform\Metadata\Operation;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\QueryBuilder;

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

$parameter = $context['parameter'];
$qb = clone $queryBuilder;
$qb->resetDQLPart('where');
$qb->setParameters(new ArrayCollection());
foreach ($this->properties ?? $parameter->getProperties() ?? [] as $property) {
$this->filter->apply(
$queryBuilder,
$qb,
$queryNameGenerator,
$resourceClass,
$operation,
['parameter' => $parameter->withProperty($property)] + $context
);
}

$queryBuilder->andWhere($qb->getDQLPart('where'));
$parameters = $queryBuilder->getParameters();

foreach ($qb->getParameters() as $p) {
$parameters->add($p);
}

$queryBuilder->setParameters($parameters);
}
}
17 changes: 17 additions & 0 deletions tests/Functional/Parameters/OrFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ChickenCoop;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector;
use Doctrine\ODM\MongoDB\MongoDBException;
use PHPUnit\Framework\Attributes\DataProvider;

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

public function testOrFilterWithAnd(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped();
}
$client = self::createClient();
$client->enableProfiler();
$client->request('GET', '/chickens?autocomplete=978020137962&chickenCoop=/chicken_coops/2');
$profile = $client->getProfile();
$this->assertResponseIsSuccessful();

/** @var DoctrineDataCollector */
$db = $profile->getCollector('db');
$this->assertStringContainsString('WHERE c1_.id = ? AND (c2_.name = ? OR c2_.ean = ?))', end($db->getQueries()['default'])['sql']);
}

public static function orFilterDataProvider(): \Generator
{
yield 'ean through autocomplete' => [
Expand Down
Loading