Skip to content

Fix filter eager loading & merge #1449 + test #1475

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

Merged
merged 3 commits into from
Nov 3, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$queryBuilderClone->andWhere($queryBuilderClone->expr()->in($originAlias, $in->getDQL()));
} else {
// Because Doctrine doesn't support WHERE ( foo, bar ) IN () (https://github.com/doctrine/doctrine2/issues/5238), we are building as many subqueries as they are identifiers
foreach ($classMetadata->identifier as $identifier) {
foreach ($classMetadata->getIdentifier() as $identifier) {
if (!$classMetadata->hasAssociation($identifier)) {
continue;
}

$replacementAlias = $queryNameGenerator->generateJoinAlias($originAlias);
$in = $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator, $originAlias, $replacementAlias);
$in->select("IDENTITY($replacementAlias.$identifier)");
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Orm/Util/EagerLoadingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $
{
$checked[] = $classMetadata->name;

foreach ($classMetadata->associationMappings as $mapping) {
foreach ($classMetadata->getAssociationMappings() as $mapping) {
if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
return true;
}
Expand Down
10 changes: 7 additions & 3 deletions src/Bridge/Doctrine/Orm/Util/QueryChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ public static function hasOrderByOnToManyJoin(QueryBuilder $queryBuilder, Manage
if (!isset($orderByAliases[$alias])) {
continue;
}

$relationship = QueryJoinParser::getJoinRelationship($join);

if (false !== strpos($relationship, '.')) {
$metadata = QueryJoinParser::getClassMetadataFromJoinAlias($alias, $queryBuilder, $managerRegistry);
if ($metadata->isCollectionValuedAssociation($relationship)) {
/*
* We select the parent alias because it may differ from the origin alias given above
* @see https://github.com/api-platform/core/issues/1313
*/
list($relationAlias, $association) = explode('.', $relationship);
$metadata = QueryJoinParser::getClassMetadataFromJoinAlias($relationAlias, $queryBuilder, $managerRegistry);
if ($metadata->isCollectionValuedAssociation($association)) {
return true;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,14 @@ public function testCompositeIdentifiers()
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(CompositeRelation::class)->willReturn(new ResourceMetadata(CompositeRelation::class));

$classMetadata = new ClassMetadataInfo(CompositeRelation::class);
$classMetadataProphecy = $this->prophesize(ClassMetadataInfo::class);
$classMetadataProphecy->getIdentifier()->willReturn(['item', 'label']);
$classMetadataProphecy->getAssociationMappings()->willReturn(['item' => ['fetch' => ClassMetadataInfo::FETCH_EAGER]]);
$classMetadataProphecy->hasAssociation('item')->shouldBeCalled()->willReturn(true);
$classMetadataProphecy->hasAssociation('label')->shouldBeCalled()->willReturn(true);

$classMetadata = $classMetadataProphecy->reveal();
$classMetadata->isIdentifierComposite = true;
$classMetadata->identifier = ['item', 'label'];

$em = $this->prophesize(EntityManager::class);
$em->getExpressionBuilder()->shouldBeCalled()->willReturn(new Expr());
Expand Down Expand Up @@ -353,6 +358,64 @@ public function testFetchEagerWithNoForceEager()
$this->assertEquals($this->toDQLString($expected), $qb->getDQL());
}

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

$classMetadataProphecy = $this->prophesize(ClassMetadataInfo::class);
$classMetadataProphecy->getIdentifier()->willReturn(['item', 'label', 'bar']);
$classMetadataProphecy->getAssociationMappings()->willReturn(['item' => ['fetch' => ClassMetadataInfo::FETCH_EAGER]]);
$classMetadataProphecy->hasAssociation('item')->shouldBeCalled()->willReturn(true);
$classMetadataProphecy->hasAssociation('label')->shouldBeCalled()->willReturn(true);
$classMetadataProphecy->hasAssociation('bar')->shouldBeCalled()->willReturn(false);

$classMetadata = $classMetadataProphecy->reveal();
$classMetadata->isIdentifierComposite = true;

$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 AND o.bar = :bar')
->setParameter('foo', 1)
->setParameter('bar', 2);

$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(), true);
$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
WHERE item_2.field1 = :foo AND o_2.bar = :bar
)) 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 AND o_2.bar = :bar
))
SQL;

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

private function toDQLString(string $dql): string
{
return preg_replace(['/\s+/', '/\(\s/', '/\s\)/'], [' ', '(', ')'], $dql);
Expand Down
21 changes: 21 additions & 0 deletions tests/Bridge/Doctrine/Orm/Util/QueryCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,25 @@ public function testHasOrderByOnToManyJoinWithClassLeftJoin()

$this->assertTrue(QueryChecker::hasOrderByOnToManyJoin($queryBuilder->reveal(), $managerRegistry->reveal()));
}

/**
* Adds a test on the fix referenced in https://github.com/api-platform/core/pull/1449.
*/
public function testOrderByOnToManyWithRelationAsBasis()
{
$queryBuilder = $this->prophesize(QueryBuilder::class);
$queryBuilder->getRootEntities()->willReturn(['Dummy']);
$queryBuilder->getRootAliases()->willReturn(['d']);
$queryBuilder->getDQLPart('join')->willReturn(['d' => [new Join('LEFT_JOIN', 'd.relatedDummy', 'a_1')]]);
$queryBuilder->getDQLPart('orderBy')->willReturn(['a_1.name' => new OrderBy('a_1.name', 'asc')]);
$classMetadata = $this->prophesize(ClassMetadata::class);
$classMetadata = $this->prophesize(ClassMetadata::class);
$classMetadata->isCollectionValuedAssociation('relatedDummy')->willReturn(true);
$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata->reveal());
$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal());

$this->assertTrue(QueryChecker::hasOrderByOnToManyJoin($queryBuilder->reveal(), $managerRegistry->reveal()));
}
}
2 changes: 1 addition & 1 deletion tests/Bridge/Doctrine/Orm/Util/QueryJoinParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testGetClassMetadataFromJoinAlias()
$queryBuilder = $this->prophesize(QueryBuilder::class);
$queryBuilder->getRootEntities()->willReturn(['Dummy']);
$queryBuilder->getRootAliases()->willReturn(['d']);
$queryBuilder->getDQLPart('join')->willReturn(['a_1' => new Join('INNER_JOIN', 'relatedDummy', 'a_1', null, 'a_1.name = r.name')]);
$queryBuilder->getDQLPart('join')->willReturn(['a_1' => [new Join('INNER_JOIN', 'relatedDummy', 'a_1', null, 'a_1.name = r.name')]]);
$classMetadata = $this->prophesize(ClassMetadata::class);
$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata->reveal());
Expand Down