Skip to content

Commit

Permalink
Prevents incorrect table aliases to be generated
Browse files Browse the repository at this point in the history
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 #8112 for the consequence of
such typo.
  • Loading branch information
gquemener committed Apr 23, 2020
1 parent 8c259ea commit 75ffa10
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\ORM\Mapping\Shape" inheritance-type="SINGLE_TABLE">
<discriminator-column name="discr" type="string" length="32" />
<discriminator-map>
<discriminator-mapping value="cube" class="Doctrine\Tests\ORM\Mapping\cube" />
</discriminator-map>
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>

0 comments on commit 75ffa10

Please sign in to comment.