diff --git a/UPGRADE.md b/UPGRADE.md index df6fcd889f6..05986037b5a 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -492,6 +492,10 @@ following classes and methods: Use `toIterable()` instead. +## BC BREAK: `Doctrine\ORM\EntityManagerInterface#getReference()` and `getPartialReference()` does not return `null` anymore + +Method `Doctrine\ORM\EntityManagerInterface#getReference()` and `getPartialReference()` throws `InstanceOfTheWrongTypeEncountered` in 3.0 when different entity type is found in inheritance hierachy. + # Upgrade to 2.14 ## Deprecated annotation mapping driver. diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 5621dd697d6..47f339fb4b8 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -12,6 +12,7 @@ use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\LockMode; use Doctrine\ORM\Exception\EntityManagerClosed; +use Doctrine\ORM\Exception\InstanceOfTheWrongTypeEncountered; use Doctrine\ORM\Exception\InvalidHydrationMode; use Doctrine\ORM\Exception\ManagerException; use Doctrine\ORM\Exception\MismatchedEventManager; @@ -402,7 +403,11 @@ public function getReference(string $entityName, $id): object|null // Check identity map first, if its already in there just return it. if ($entity !== false) { - return $entity instanceof $class->name ? $entity : null; + if (! $entity instanceof $class->name) { + throw InstanceOfTheWrongTypeEncountered::forInstance($entity); + } + + return $entity; } if ($class->subClasses) { @@ -427,7 +432,11 @@ public function getPartialReference(string $entityName, $identifier): object|nul // Check identity map first, if its already in there just return it. if ($entity !== false) { - return $entity instanceof $class->name ? $entity : null; + if (! $entity instanceof $class->name) { + throw InstanceOfTheWrongTypeEncountered::forInstance($entity); + } + + return $entity; } if (! is_array($identifier)) { diff --git a/lib/Doctrine/ORM/EntityManagerInterface.php b/lib/Doctrine/ORM/EntityManagerInterface.php index fa445f8572a..33990dc39f2 100644 --- a/lib/Doctrine/ORM/EntityManagerInterface.php +++ b/lib/Doctrine/ORM/EntityManagerInterface.php @@ -140,7 +140,7 @@ public function find(string $className, mixed $id, LockMode|int|null $lockMode = * @param mixed $id The entity identifier. * @psalm-param class-string $entityName * - * @psalm-return T|null + * @psalm-return T * * @throws ORMException * @@ -167,7 +167,7 @@ public function getReference(string $entityName, $id): object|null; * @param mixed $identifier The entity identifier. * @psalm-param class-string $entityName * - * @psalm-return T|null + * @psalm-return T * * @template T of object */ diff --git a/lib/Doctrine/ORM/Exception/InstanceOfTheWrongTypeEncountered.php b/lib/Doctrine/ORM/Exception/InstanceOfTheWrongTypeEncountered.php new file mode 100644 index 00000000000..02eafa238d4 --- /dev/null +++ b/lib/Doctrine/ORM/Exception/InstanceOfTheWrongTypeEncountered.php @@ -0,0 +1,19 @@ +$entity $entity $entity - $entity instanceof $class->name ? $entity : null - $entity instanceof $class->name ? $entity : null + $entity + $entity $persister->load($sortedId, null, null, [], $lockMode) $persister->loadById($sortedId) $this->metadataFactory diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php index 19f96242610..817c9ab99db 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; +use Doctrine\ORM\Exception\InstanceOfTheWrongTypeEncountered; use Doctrine\Tests\Models\Company\CompanyFixContract; use Doctrine\Tests\Models\Company\CompanyFlexContract; use Doctrine\Tests\OrmFunctionalTestCase; @@ -17,7 +18,7 @@ protected function setUp(): void parent::setUp(); } - public function testGrabWrongSubtypeReturnsNull(): void + public function testGrabWrongSubtypeReturnsNullOrThrows(): void { $fix = new CompanyFixContract(); $fix->setFixPrice(2000); @@ -28,7 +29,15 @@ public function testGrabWrongSubtypeReturnsNull(): void $id = $fix->getId(); self::assertNull($this->_em->find(CompanyFlexContract::class, $id)); - self::assertNull($this->_em->getReference(CompanyFlexContract::class, $id)); - self::assertNull($this->_em->getPartialReference(CompanyFlexContract::class, $id)); + + try { + $this->_em->getReference(CompanyFlexContract::class, $id); + } catch (InstanceOfTheWrongTypeEncountered) { + } + + try { + $this->_em->getPartialReference(CompanyFlexContract::class, $id); + } catch (InstanceOfTheWrongTypeEncountered) { + } } }