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
8 changes: 2 additions & 6 deletions src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,9 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator

$forceEager = $this->isForceEager($resourceClass, $options);

try {
$groups = $this->getSerializerGroups($resourceClass, $options, 'normalization_context');
$groups = $this->getSerializerGroups($resourceClass, $options, 'normalization_context');

$this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $queryBuilder->getRootAliases()[0], $groups);
} catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
//ignore the not found exception
}
$this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $queryBuilder->getRootAliases()[0], $groups);
}

/**
Expand Down
32 changes: 29 additions & 3 deletions src/Bridge/Doctrine/Orm/Extension/FilterEagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

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

Expand All @@ -36,7 +38,10 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
*/
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if (false === $this->forceEager || false === $this->isForceEager($resourceClass, ['collection_operation_name' => $operationName])) {
$em = $queryBuilder->getEntityManager();
$classMetadata = $em->getClassMetadata($resourceClass);

if (!$this->hasFetchEagerAssociation($em, $classMetadata) && (false === $this->forceEager || false === $this->isForceEager($resourceClass, ['collection_operation_name' => $operationName]))) {
return;
}

Expand All @@ -57,8 +62,6 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$queryBuilderClone = clone $queryBuilder;
$queryBuilderClone->resetDQLPart('where');

$classMetadata = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);

if (!$classMetadata->isIdentifierComposite) {
$replacementAlias = $queryNameGenerator->generateJoinAlias($originAlias);
$in = $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator, $originAlias, $replacementAlias);
Expand Down Expand Up @@ -143,4 +146,27 @@ private function isForceEager(string $resourceClass, array $options): bool

return is_bool($forceEager) ? $forceEager : $this->forceEager;
}

private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $classMetadata, &$checked = [])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to extract this logic out for reuse between EagerLoadingExtension and FilterEagerLoadingExtension?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to to this to avoid a 2-pass recursive call in EagerLoadingExtension. If we want to extract this logic we need to add cache. I obviously agree that we should add a new class with the shouldEagerLoad logic.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be extracted in another PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it can and should, +1 to do this in another PR as it's not a priority.

{
$checked[] = $classMetadata->name;

foreach ($classMetadata->associationMappings as $mapping) {
if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
return true;
}

$related = $em->getClassMetadata($mapping['targetEntity']);

if (in_array($related->name, $checked)) {
continue;
}

if (true === $this->hasFetchEagerAssociation($em, $related, $checked)) {
return true;
}
}

return false;
}
}
123 changes: 100 additions & 23 deletions tests/Bridge/Doctrine/Orm/Extension/FilterEagerLoadingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Foo;
Expand All @@ -37,8 +39,12 @@ public function testIsNoForceEagerCollectionAttributes()
],
], null));

$em = $this->prophesize(EntityManager::class);
$em->getClassMetadata(DummyCar::class)->shouldBeCalled()->willReturn(new ClassMetadataInfo(DummyCar::class));

$qb = $this->prophesize(QueryBuilder::class);
$qb->getDQLPart('where')->shouldNotBeCalled();
$qb->getEntityManager()->willReturn($em);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);

Expand All @@ -48,13 +54,17 @@ public function testIsNoForceEagerCollectionAttributes()

public function testIsNoForceEagerResource()
{
$em = $this->prophesize(EntityManager::class);
$em->getClassMetadata(DummyCar::class)->shouldBeCalled()->willReturn(new ClassMetadataInfo(DummyCar::class));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(DummyCar::class)->willReturn(new ResourceMetadata(DummyCar::class, null, null, null, [
'get' => [],
], ['force_eager' => false]));

$qb = $this->prophesize(QueryBuilder::class);
$qb->getDQLPart('where')->shouldNotBeCalled();
$qb->getEntityManager()->willReturn($em);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);

Expand All @@ -64,13 +74,17 @@ public function testIsNoForceEagerResource()

public function testIsForceEagerConfig()
{
$em = $this->prophesize(EntityManager::class);
$em->getClassMetadata(DummyCar::class)->shouldBeCalled()->willReturn(new ClassMetadataInfo(DummyCar::class));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(DummyCar::class)->willReturn(new ResourceMetadata(DummyCar::class, null, null, null, [
'get' => [],
]));

$qb = $this->prophesize(QueryBuilder::class);
$qb->getDQLPart('where')->shouldNotBeCalled();
$qb->getEntityManager()->willReturn($em);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);

Expand All @@ -80,11 +94,15 @@ public function testIsForceEagerConfig()

public function testHasNoWherePart()
{
$em = $this->prophesize(EntityManager::class);
$em->getClassMetadata(DummyCar::class)->shouldBeCalled()->willReturn(new ClassMetadataInfo(DummyCar::class));

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

$qb = $this->prophesize(QueryBuilder::class);
$qb->getDQLPart('where')->shouldBeCalled()->willReturn(null);
$qb->getEntityManager()->willReturn($em);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);

Expand All @@ -94,12 +112,16 @@ public function testHasNoWherePart()

public function testHasNoJoinPart()
{
$em = $this->prophesize(EntityManager::class);
$em->getClassMetadata(DummyCar::class)->shouldBeCalled()->willReturn(new ClassMetadataInfo(DummyCar::class));

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

$qb = $this->prophesize(QueryBuilder::class);
$qb->getDQLPart('where')->shouldBeCalled()->willReturn(new Expr\Andx());
$qb->getDQLPart('join')->shouldBeCalled()->willReturn(null);
$qb->getEntityManager()->willReturn($em);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);

Expand Down Expand Up @@ -162,12 +184,12 @@ public function testHiddenOrderBy()
$filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), DummyCar::class, 'get');

$expected = <<<SQL
SELECT o, CASE WHEN o.dateCreated IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dateCreated_null_rank
FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
LEFT JOIN o.colors colors
SELECT o, CASE WHEN o.dateCreated IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dateCreated_null_rank
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
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
) ORDER BY _o_dateCreated_null_rank DESC ASC
SQL;
Expand Down Expand Up @@ -202,15 +224,15 @@ public function testGroupBy()
$filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), DummyCar::class, 'get');

$expected = <<<SQL
SELECT o, count(o.id) as counter
FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
LEFT JOIN o.colors colors WHERE o
SELECT o, count(o.id) as counter
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
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
)
GROUP BY o.colors HAVING counter > 3
)
GROUP BY o.colors HAVING counter > 3
ORDER BY o.colors ASC
SQL;

Expand Down Expand Up @@ -248,19 +270,74 @@ public function testCompositeIdentifiers()
$filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), CompositeRelation::class, 'get');

$expected = <<<SQL
SELECT o
FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
INNER JOIN o.compositeItem item
INNER JOIN o.compositeLabel label
SELECT o
FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
INNER JOIN o.compositeItem item
INNER JOIN o.compositeLabel label
WHERE o.item IN(
SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
INNER JOIN o_2.compositeItem item_2
INNER JOIN o_2.compositeLabel label_2
WHERE item_2.field1 = :foo
) AND o.label IN(
SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
INNER JOIN o_2.compositeItem item_2
INNER JOIN o_2.compositeLabel label_2
WHERE item_2.field1 = :foo
)
SQL;

$this->assertEquals($this->toDQLString($expected), $qb->getDQL());
}

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

$classMetadata = new ClassMetadataInfo(CompositeRelation::class);
$classMetadata->isIdentifierComposite = true;
$classMetadata->identifier = ['item', 'label'];
$classMetadata->associationMappings = [
'item' => ['fetch' => 3, 'joinColumns' => [['nullable' => false]], 'targetEntity' => CompositeItem::class],
'label' => ['fetch' => 3, 'joinColumns' => [['nullable' => false]], 'targetEntity' => CompositeLabel::class],
];

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

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

$qb->select('o')
->from(CompositeRelation::class, 'o')
->innerJoin('o.compositeItem', 'item')
->innerJoin('o.compositeLabel', 'label')
->where('item.field1 = :foo')
->setParameter('foo', 1);

$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class);
$queryNameGenerator->generateJoinAlias('item')->shouldBeCalled()->willReturn('item_2');
$queryNameGenerator->generateJoinAlias('label')->shouldBeCalled()->willReturn('label_2');
$queryNameGenerator->generateJoinAlias('o')->shouldBeCalled()->willReturn('o_2');

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

$expected = <<<SQL
SELECT o
FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
INNER JOIN o.compositeItem item
INNER JOIN o.compositeLabel label
WHERE o.item IN(
SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
INNER JOIN o_2.compositeItem item_2
INNER JOIN o_2.compositeLabel label_2
SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
INNER JOIN o_2.compositeItem item_2
INNER JOIN o_2.compositeLabel label_2
WHERE item_2.field1 = :foo
) AND o.label IN(
SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
INNER JOIN o_2.compositeItem item_2
INNER JOIN o_2.compositeLabel label_2
SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
INNER JOIN o_2.compositeItem item_2
INNER JOIN o_2.compositeLabel label_2
WHERE item_2.field1 = :foo
)
SQL;
Expand All @@ -270,6 +347,6 @@ public function testCompositeIdentifiers()

private function toDQLString(string $dql): string
{
return preg_replace('/\\r\\n|\\n/', '', str_replace(' ', '', $dql));
return preg_replace(['/\s+/', '/\(\s/', '/\s\)/'], [' ', '(', ')'], $dql);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be better \o/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is unsafe... It might change whitespace within a SQL literal (string). Lol...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, as said before it makes tests more readable :).

}
}