From 75ffa10b445edcc7247da65ff326dac33d768125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gildas=20Qu=C3=A9m=C3=A9ner?= Date: Thu, 23 Apr 2020 10:57:38 +0200 Subject: [PATCH] Prevents incorrect table aliases to be generated When a defined subclass has a case typo, the query builder will be lost and will generate exotic table alias. This commit fixes the issue at the root by prohibiting case typo in discriminator map. See https://github.com/doctrine/orm/pull/8112 for the consequence of such typo. --- lib/Doctrine/ORM/Mapping/ClassMetadata.php | 6 ++++ .../ORM/Mapping/AbstractMappingDriverTest.php | 28 +++++++++++++++++++ .../Doctrine.Tests.ORM.Mapping.Shape.dcm.xml | 15 ++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Shape.dcm.xml diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index 557627e16d6..7d0feb24ee8 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -10,6 +10,7 @@ use Doctrine\ORM\Reflection\ReflectionService; use Doctrine\ORM\Sequencing\Planning\ValueGenerationPlan; use Doctrine\ORM\Utility\PersisterHelper; +use ReflectionClass; use ReflectionException; use RuntimeException; use function array_filter; @@ -1002,6 +1003,11 @@ public function addDiscriminatorMapClass($name, string $className) : void throw MappingException::invalidClassInDiscriminatorMap($className, $this->className); } + $refl = new ReflectionClass($className); + if ($refl->name !== $className) { + throw MappingException::invalidClassInDiscriminatorMap($className, $this->className); + } + if (is_subclass_of($className, $this->className) && ! in_array($className, $this->subClasses, true)) { $this->subClasses[] = $className; } diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 9232baf395b..25913f37632 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -1081,6 +1081,15 @@ public function testDiscriminatorColumnDefaultName() : void self::assertEquals('dtype', $class->discriminatorColumn->getColumnName()); } + + /** + * @expectedException \Doctrine\ORM\Mapping\MappingException + * @expectedExceptionMessage Entity class 'Doctrine\Tests\ORM\Mapping\cube' used in the discriminator map of class 'Doctrine\Tests\ORM\Mapping\Shape' does not exist. + */ + public function testInvalidSubClassCase() + { + $this->createClassMetadata(Shape::class); + } } /** @@ -1344,6 +1353,25 @@ class Dog extends Animal { } +/** + * @ORM\Entity + * @ORM\InheritanceType("SINGLE_TABLE") + * @ORM\DiscriminatorMap({"cube" = cube::class}) + * @ORM\DiscriminatorColumn(name="discr", length=32, type="string") + */ +abstract class Shape +{ + /** + * @ORM\Id @ORM\Column(type="string") @ORM\GeneratedValue(strategy="AUTO") + */ + public $id; +} + +/** @ORM\Entity */ +class Cube extends Shape +{ +} + /** * @ORM\Entity */ diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Shape.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Shape.dcm.xml new file mode 100644 index 00000000000..9ca16f26804 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Shape.dcm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + +