Skip to content

Commit

Permalink
Revert "fix: missing eager joins on to-one relationships (#5992)"
Browse files Browse the repository at this point in the history
This reverts commit 9848bd4.
  • Loading branch information
soyuka committed Nov 27, 2023
1 parent 7837e09 commit dac49cb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 41 deletions.
3 changes: 1 addition & 2 deletions features/json/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Feature: JSON relations support
"badFourthLevel": null,
"id": 1,
"level": 3,
"test": true,
"relatedDummies": []
"test": true
}
"""

Expand Down
1 change: 0 additions & 1 deletion src/Doctrine/Orm/Extension/EagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
if (
null !== $parentAssociation
&& isset($mapping['inversedBy'])
&& $mapping['sourceEntity'] === $mapping['targetEntity']
&& $mapping['inversedBy'] === $parentAssociation
&& $mapping['type'] & ClassMetadata::TO_ONE
) {
Expand Down
35 changes: 8 additions & 27 deletions tests/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\EmbeddableDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ThirdLevel;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\UnknownDummy;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -141,7 +140,6 @@ public function testApplyToItem(): void
$propertyNameCollectionFactoryProphecy->create(RelatedDummy::class)->willReturn($relatedNameCollection)->shouldBeCalled();
$propertyNameCollectionFactoryProphecy->create(EmbeddableDummy::class)->willReturn($relatedEmbedableCollection)->shouldBeCalled();
$propertyNameCollectionFactoryProphecy->create(UnknownDummy::class)->willReturn(new PropertyNameCollection(['id']))->shouldBeCalled();
$propertyNameCollectionFactoryProphecy->create(ThirdLevel::class)->willReturn(new PropertyNameCollection(['id']))->shouldBeCalled();

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$relationPropertyMetadata = new ApiProperty();
Expand All @@ -153,7 +151,6 @@ public function testApplyToItem(): void
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy4', $callContext)->willReturn($relationPropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy5', $callContext)->willReturn($relationPropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(Dummy::class, 'singleInheritanceRelation', $callContext)->willReturn($relationPropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', $callContext)->willReturn($relationPropertyMetadata)->shouldBeCalled();

$idPropertyMetadata = new ApiProperty();
$idPropertyMetadata = $idPropertyMetadata->withIdentifier(true);
Expand All @@ -172,9 +169,7 @@ public function testApplyToItem(): void
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'notindatabase', $callContext)->willReturn($notInDatabasePropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'notreadable', $callContext)->willReturn($notReadablePropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'relation', $callContext)->willReturn($relationPropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'thirdLevel', $callContext)->willReturn($relationPropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(UnknownDummy::class, 'id', $callContext)->willReturn($idPropertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(ThirdLevel::class, 'id', $callContext)->willReturn($idPropertyMetadata)->shouldBeCalled();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);

Expand All @@ -186,7 +181,6 @@ public function testApplyToItem(): void
'relatedDummy4' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'targetEntity' => UnknownDummy::class],
'relatedDummy5' => ['fetch' => ClassMetadataInfo::FETCH_LAZY, 'targetEntity' => UnknownDummy::class],
'singleInheritanceRelation' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'targetEntity' => AbstractDummy::class],
'relatedDummies' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'targetEntity' => RelatedDummy::class],
];

$relatedClassMetadataProphecy = $this->prophesize(ClassMetadata::class);
Expand All @@ -200,7 +194,6 @@ public function testApplyToItem(): void

$relatedClassMetadataProphecy->associationMappings = [
'relation' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'joinColumns' => [['nullable' => false]], 'targetEntity' => UnknownDummy::class],
'thirdLevel' => ['fetch' => ClassMetadataInfo::FETCH_EAGER, 'targetEntity' => ThirdLevel::class, 'sourceEntity' => RelatedDummy::class, 'inversedBy' => 'relatedDummies', 'type' => ClassMetadata::TO_ONE],
];

$relatedClassMetadataProphecy->embeddedClasses = ['embeddedDummy' => ['class' => EmbeddableDummy::class]];
Expand All @@ -211,38 +204,26 @@ public function testApplyToItem(): void
$unknownClassMetadataProphecy = $this->prophesize(ClassMetadata::class);
$unknownClassMetadataProphecy->associationMappings = [];

$thirdLevelMetadataProphecy = $this->prophesize(ClassMetadata::class);
$thirdLevelMetadataProphecy->associationMappings = [];

$emProphecy = $this->prophesize(EntityManager::class);
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
$emProphecy->getClassMetadata(RelatedDummy::class)->shouldBeCalled()->willReturn($relatedClassMetadataProphecy->reveal());
$emProphecy->getClassMetadata(AbstractDummy::class)->shouldBeCalled()->willReturn($singleInheritanceClassMetadataProphecy->reveal());
$emProphecy->getClassMetadata(UnknownDummy::class)->shouldBeCalled()->willReturn($unknownClassMetadataProphecy->reveal());
$emProphecy->getClassMetadata(ThirdLevel::class)->shouldBeCalled()->willReturn($thirdLevelMetadataProphecy->reveal());

$queryBuilderProphecy->getRootAliases()->willReturn(['o']);
$queryBuilderProphecy->getEntityManager()->willReturn($emProphecy);
$queryBuilderProphecy->leftJoin('o.relatedDummy', 'relatedDummy_a1')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('relatedDummy_a1.relation', 'relation_a2')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('relatedDummy_a1.thirdLevel', 'thirdLevel_a3')->shouldBeCalledTimes(1);
$queryBuilderProphecy->innerJoin('o.relatedDummy2', 'relatedDummy2_a4')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.relatedDummy3', 'relatedDummy3_a5')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.relatedDummy4', 'relatedDummy4_a6')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.singleInheritanceRelation', 'singleInheritanceRelation_a7')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.relatedDummies', 'relatedDummies_a8')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('relatedDummies_a8.relation', 'relation_a9')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('relatedDummies_a8.thirdLevel', 'thirdLevel_a10')->shouldBeCalledTimes(1);
$queryBuilderProphecy->innerJoin('o.relatedDummy2', 'relatedDummy2_a3')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.relatedDummy3', 'relatedDummy3_a4')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.relatedDummy4', 'relatedDummy4_a5')->shouldBeCalledTimes(1);
$queryBuilderProphecy->leftJoin('o.singleInheritanceRelation', 'singleInheritanceRelation_a6')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy_a1.{id,name,embeddedDummy.name}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial thirdLevel_a3.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relation_a2.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy2_a4.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy3_a5.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy4_a6.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('singleInheritanceRelation_a7')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummies_a8.{id,name,embeddedDummy.name}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relation_a9.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial thirdLevel_a10.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy2_a3.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy3_a4.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('partial relatedDummy4_a5.{id}')->shouldBeCalledTimes(1);
$queryBuilderProphecy->addSelect('singleInheritanceRelation_a6')->shouldBeCalledTimes(1);
$queryBuilderProphecy->getDQLPart('join')->willReturn([]);
$queryBuilderProphecy->getDQLPart('select')->willReturn([]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/RelatedDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RelatedDummy extends ParentDummy implements \Stringable
#[ApiFilter(filterClass: DateFilter::class)]
public $dummyDate;

#[ORM\ManyToOne(targetEntity: ThirdLevel::class, cascade: ['persist'], inversedBy: 'relatedDummies')]
#[ORM\ManyToOne(targetEntity: ThirdLevel::class, cascade: ['persist'])]
#[Groups(['barcelona', 'chicago', 'friends'])]
public ?ThirdLevel $thirdLevel = null;

Expand Down
10 changes: 0 additions & 10 deletions tests/Fixtures/TestBundle/Entity/ThirdLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Link;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

Expand Down Expand Up @@ -53,14 +51,6 @@ class ThirdLevel
#[ORM\ManyToOne(targetEntity: FourthLevel::class, cascade: ['persist'])]
public $badFourthLevel;

#[ORM\OneToMany(mappedBy: 'thirdLevel', targetEntity: RelatedDummy::class)]
public Collection|iterable $relatedDummies;

public function __construct()
{
$this->relatedDummies = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
Expand Down

0 comments on commit dac49cb

Please sign in to comment.