Skip to content

Avoid to truncate WHERE clauses when clonning the querybuilder #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions src/Bridge/Doctrine/Orm/Extension/FilterEagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;

Expand Down Expand Up @@ -102,8 +103,17 @@ private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, Query
}

//Change where aliases
foreach ($wherePart->getParts() as $where) {
$queryBuilderClone->add('where', str_replace($aliases, $replacements, $where));
switch (true) {
case $wherePart instanceof Expr\Orx:
foreach ($wherePart->getParts() as $where) {
$queryBuilderClone->orWhere(str_replace($aliases, $replacements, $where));
}
break;
case $wherePart instanceof Expr\Andx:
foreach ($wherePart->getParts() as $where) {
$queryBuilderClone->andWhere(str_replace($aliases, $replacements, $where));
}
break;
}

return $queryBuilderClone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,58 @@ public function testApplyCollection()

$this->assertEquals('SELECT o FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o LEFT JOIN o.colors colors WHERE o IN(SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2 LEFT JOIN o_2.colors colors_2 WHERE o_2.colors = :foo)', $qb->getDQL());
}

public function testApplyCollectionWithWhereOrClauses()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(DummyCar::class)->willReturn(new ResourceMetadata(DummyCar::class));

$em = $this->prophesize(EntityManager::class);
$em->getExpressionBuilder()->shouldBeCalled()->willReturn(new Expr());

$qb = new QueryBuilder($em->reveal());

$qb->select('o')
->from(DummyCar::class, 'o')
->leftJoin('o.colors', 'colors')
->where('o.colors = :foo')
->orWhere('o.colors = :bar')
->setParameter('foo', 1)
->setParameter('bar', 2);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);
$queryNameGenerator->generateJoinAlias('colors')->shouldBeCalled()->willReturn('colors_2');

$filterEagerLoadingExtension = new FilterEagerLoadingExtension($resourceMetadataFactoryProphecy->reveal(), true);
$filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), DummyCar::class, 'get');

$this->assertEquals('SELECT o FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o LEFT JOIN o.colors colors WHERE o IN(SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2 LEFT JOIN o_2.colors colors_2 WHERE o_2.colors = :foo OR o_2.colors = :bar)', $qb->getDQL());
}

public function testApplyCollectionWithWhereAndClauses()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(DummyCar::class)->willReturn(new ResourceMetadata(DummyCar::class));

$em = $this->prophesize(EntityManager::class);
$em->getExpressionBuilder()->shouldBeCalled()->willReturn(new Expr());

$qb = new QueryBuilder($em->reveal());

$qb->select('o')
->from(DummyCar::class, 'o')
->leftJoin('o.colors', 'colors')
->where('o.colors = :foo')
->andWhere('o.colors = :bar')
->setParameter('foo', 1)
->setParameter('bar', 2);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);
$queryNameGenerator->generateJoinAlias('colors')->shouldBeCalled()->willReturn('colors_2');

$filterEagerLoadingExtension = new FilterEagerLoadingExtension($resourceMetadataFactoryProphecy->reveal(), true);
$filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), DummyCar::class, 'get');

$this->assertEquals('SELECT o FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o LEFT JOIN o.colors colors WHERE o IN(SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2 LEFT JOIN o_2.colors colors_2 WHERE o_2.colors = :foo AND o_2.colors = :bar)', $qb->getDQL());
}
}