Skip to content

Commit

Permalink
doctrine#11090 - Fix obtaining an identifier in cases where the hydra…
Browse files Browse the repository at this point in the history
…tion has not yet fully completed on eagerLoadCollections
  • Loading branch information
dbannik committed Feb 15, 2024
1 parent 1d218ba commit c586e4b
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -3252,7 +3252,17 @@ private function eagerLoadCollections(array $collections, array $mapping): void
foreach ($found as $targetValue) {
$sourceEntity = $targetProperty->getValue($targetValue);

$id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($sourceEntity));
// In cases where the hydration $targetValue has not yet fully completed
if ($sourceEntity === null && isset($targetClass->associationMappings[$mappedBy]['joinColumns'])) {
$data = $this->getOriginalEntityData($targetValue);
$id = [];
foreach ($targetClass->associationMappings[$mappedBy]['joinColumns'] as $joinColumn) {
$id[] = $data[$joinColumn['name']];
}
} else {
$id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($sourceEntity));
}

$idHash = implode(' ', $id);

if (isset($mapping['indexBy'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"mobile"="MobileRemoteControl"})
*/
abstract class AbstractRemoveControl
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* /**
*
* @ORM\Column(type="string")
*
* @var string
*/
public $name;

/**
* @ORM\OneToMany(targetEntity="User", mappedBy="remoteControl", fetch="EAGER")
*
* @var Collection<User>
*/
public $users;

public function __construct(string $name)
{
$this->name = $name;
$this->users = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class MobileRemoteControl extends AbstractRemoveControl
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="AbstractRemoveControl", inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*
* @var AbstractRemoveControl
*/
public $remoteControl;

public function __construct(AbstractRemoveControl $control)
{
$this->remoteControl = $control;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"mobile"="MobileRemoteControl"})
*/
abstract class AbstractRemoveControl
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* /**
*
* @ORM\Column(type="string")
*
* @var string
*/
public $name;

/**
* @ORM\OneToMany(targetEntity="User", mappedBy="remoteControl")
*
* @var Collection<User>
*/
public $users;

public function __construct(string $name)
{
$this->name = $name;
$this->users = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class MobileRemoteControl extends AbstractRemoveControl
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="AbstractRemoveControl", inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*
* @var AbstractRemoveControl
*/
public $remoteControl;

public function __construct(AbstractRemoveControl $control)
{
$this->remoteControl = $control;
}
}
61 changes: 61 additions & 0 deletions tests/Tests/ORM/Functional/AbstractFetchEagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager\AbstractRemoveControl;
use Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager\MobileRemoteControl;
use Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager\User;
use Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager\AbstractRemoveControl as AbstractRemoveControlWithoutFetchEager;
use Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager\MobileRemoteControl as MobileRemoteControlWithoutFetchEager;
use Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager\User as UserWithoutFetchEager;
use PHPUnit\Framework\TestCase;

final class AbstractFetchEagerTest extends TestCase
{
public function testWithAbstractFetchEager(): void
{
$entityManager = EntityManagerFactory::getEntityManager([
AbstractRemoveControl::class,
User::class,
]);

$control = new MobileRemoteControl('smart');
$user = new User($control);

$entityManager->persist($control);
$entityManager->persist($user);
$entityManager->flush();
$entityManager->clear();

$user = $entityManager->find(User::class, $user->id);

self::assertNotNull($user);
self::assertEquals('smart', $user->remoteControl->name);
self::assertTrue($user->remoteControl->users->contains($user));
}

public function testWithoutAbstractFetchEager(): void
{
$entityManager = EntityManagerFactory::getEntityManager([
AbstractRemoveControlWithoutFetchEager::class,
UserWithoutFetchEager::class,
]);

$control = new MobileRemoteControlWithoutFetchEager('smart');
$user = new UserWithoutFetchEager($control);

$entityManager->persist($control);
$entityManager->persist($user);
$entityManager->flush();
$entityManager->clear();

$user = $entityManager->find(UserWithoutFetchEager::class, $user->id);

self::assertNotNull($user);
self::assertEquals('smart', $user->remoteControl->name);
self::assertTrue($user->remoteControl->users->contains($user));
}
}

0 comments on commit c586e4b

Please sign in to comment.