Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,20 @@ public function setDiscriminatorColumn(DiscriminatorColumnMapping|array|null $co
throw MappingException::invalidDiscriminatorColumnType($this->name, $columnDef['type']);
}

if (isset($columnDef['enumType'])) {
if (! enum_exists($columnDef['enumType'])) {
throw MappingException::nonEnumTypeMapped($this->name, $columnDef['fieldName'], $columnDef['enumType']);
}

if (
defined('Doctrine\DBAL\Types\Types::ENUM')
&& $columnDef['type'] === Types::ENUM
&& ! isset($columnDef['options']['values'])
) {
$columnDef['options']['values'] = array_column($columnDef['enumType']::cases(), 'value');
}
}

$this->discriminatorColumn = DiscriminatorColumnMapping::fromMappingArray($columnDef);
}
}
Expand All @@ -2222,6 +2236,8 @@ final public function getDiscriminatorColumn(): DiscriminatorColumnMapping
* Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
*
* @param array<int|string, string> $map
*
* @throws MappingException
*/
public function setDiscriminatorMap(array $map): void
{
Expand All @@ -2241,6 +2257,16 @@ public function setDiscriminatorMap(array $map): void
);
}

$values = $this->discriminatorColumn->options['values'] ?? null;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now thinking we should wrap these checks in if ($this->discriminatorColumn?->enumType !== null), otherwise DiscriminatorColumn without enumType, but with options: ['values' => ['...']] will fail. Though values probably shouldn't be used without.


if ($values !== null) {
$diff = array_diff(array_keys($map), $values);

if ($diff !== []) {
throw MappingException::invalidEntriesInDiscriminatorMap(array_values($diff), $this->name, $this->discriminatorColumn->enumType);
}
}

foreach ($map as $value => $className) {
$this->addDiscriminatorMapClass($value, $className);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ public static function fileMappingDriversRequireConfiguredDirectoryPath(string|n
);
}

/**
* Returns an exception that indicates that discriminator entries used in a discriminator map
* does not exist in the backed enum provided by enumType option.
*
* @param array<int,int|string> $entries The discriminator entries that could not be found.
* @param string $owningClass The class that declares the discriminator map.
* @param string $enumType The enum that entries were checked against.
*/
public static function invalidEntriesInDiscriminatorMap(array $entries, string $owningClass, string $enumType): self
{
return new self(sprintf(
"The entries %s in the discriminator map of class '%s' do not correspond to enum cases of '%s'.",
implode(', ', array_map(static fn ($entry): string => sprintf("'%s'", $entry), $entries)),
$owningClass,
$enumType,
));
}

/**
* Returns an exception that indicates that a class used in a discriminator map does not exist.
* An example would be an outdated (maybe renamed) classname.
Expand Down
35 changes: 35 additions & 0 deletions tests/Tests/ORM/Tools/SchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\PrimaryKeyConstraintEditor;
use Doctrine\DBAL\Schema\Table as DbalTable;
use Doctrine\DBAL\Types\EnumType;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
Expand Down Expand Up @@ -46,6 +48,7 @@
use Doctrine\Tests\Models\Enums\Suit;
use Doctrine\Tests\Models\Forum\ForumAvatar;
use Doctrine\Tests\Models\Forum\ForumUser;
use Doctrine\Tests\Models\GH10288\GH10288People;
use Doctrine\Tests\Models\NullDefault\NullDefaultColumn;
use Doctrine\Tests\OrmTestCase;
use PHPUnit\Framework\Attributes\Group;
Expand Down Expand Up @@ -272,6 +275,38 @@ public function testSetDiscriminatorColumnWithoutLength(): void
self::assertEquals(255, $column->getLength());
}

public function testSetDiscriminatorColumnWithEnumType(): void
{
if (! class_exists(EnumType::class)) {
self::markTestSkipped('Test valid for doctrine/dbal versions with EnumType only.');
}

$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);
$metadata = $em->getClassMetadata(FirstEntity::class);

$metadata->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
$metadata->setDiscriminatorColumn([
'name' => 'discriminator',
'type' => Types::ENUM,
'enumType' => GH10288People::class,
]);

$schema = $schemaTool->getSchemaFromMetadata([$metadata]);

self::assertTrue($schema->hasTable('first_entity'));
$table = $schema->getTable('first_entity');

self::assertTrue($table->hasColumn('discriminator'));
$column = $table->getColumn('discriminator');
self::assertEquals(GH10288People::class, $column->getPlatformOption('enumType'));
self::assertEquals([0 => 'boss', 1 => 'employee'], $column->getValues());

$this->expectException(MappingException::class);
$this->expectExceptionMessage("The entries 'user' in the discriminator map of class '" . FirstEntity::class . "' do not correspond to enum cases of '" . GH10288People::class . "'.");
$metadata->setDiscriminatorMap(['user' => CmsUser::class, 'employee' => CmsEmployee::class]);
}

public function testDerivedCompositeKey(): void
{
$em = $this->getTestEntityManager();
Expand Down
Loading