Skip to content

Commit 5013d5d

Browse files
authored
Merge pull request #9760 from beberlei/GH-3519-UniqueDiscriminatorValues
[GH-3519] Deprecate passing the same class with different discriminator values.
2 parents f41dc4a + da51234 commit 5013d5d

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

UPGRADE.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Upgrade to 3.4
2+
3+
Using the same class several times in a discriminator map is deprecated.
4+
In 4.0, this will be an error.
5+
16
# Upgrade to 3.3
27

38
## Deprecate `DatabaseDriver`
@@ -737,7 +742,7 @@ Use `toIterable()` instead.
737742

738743
Output walkers should implement the new `\Doctrine\ORM\Query\OutputWalker` interface and create
739744
`Doctrine\ORM\Query\Exec\SqlFinalizer` instances instead of `Doctrine\ORM\Query\Exec\AbstractSqlExecutor`s.
740-
The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
745+
The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
741746
`SqlFinalizer` can be kept in the query cache and used regardless of the actual `firstResult`/`maxResult` values.
742747
Any operation dependent on `firstResult`/`maxResult` should take place within the `SqlFinalizer::createExecutor()`
743748
method. Details can be found at https://github.com/doctrine/orm/pull/11188.
@@ -750,7 +755,7 @@ change in behavior.
750755

751756
Progress on this is tracked at https://github.com/doctrine/orm/issues/11624 .
752757

753-
## PARTIAL DQL syntax is undeprecated
758+
## PARTIAL DQL syntax is undeprecated
754759

755760
Use of the PARTIAL keyword is not deprecated anymore in DQL, because we will be
756761
able to support PARTIAL objects with PHP 8.4 Lazy Objects and

src/Mapping/ClassMetadata.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
use Stringable;
2626

2727
use function array_column;
28+
use function array_count_values;
2829
use function array_diff;
30+
use function array_filter;
31+
use function array_flip;
2932
use function array_intersect;
3033
use function array_key_exists;
3134
use function array_keys;
@@ -39,6 +42,7 @@
3942
use function defined;
4043
use function enum_exists;
4144
use function explode;
45+
use function implode;
4246
use function in_array;
4347
use function interface_exists;
4448
use function is_string;
@@ -2178,6 +2182,22 @@ final public function getDiscriminatorColumn(): DiscriminatorColumnMapping
21782182
*/
21792183
public function setDiscriminatorMap(array $map): void
21802184
{
2185+
if (count(array_flip($map)) !== count($map)) {
2186+
Deprecation::trigger(
2187+
'doctrine/orm',
2188+
'https://github.com/doctrine/orm/issues/3519',
2189+
<<<'DEPRECATION'
2190+
Mapping a class to multiple discriminator values is deprecated,
2191+
and the discriminator mapping of %s contains duplicate values
2192+
for the following discriminator values: %s.
2193+
DEPRECATION,
2194+
$this->name,
2195+
implode(', ', array_keys(array_filter(array_count_values($map), static function (int $value): bool {
2196+
return $value > 1;
2197+
}))),
2198+
);
2199+
}
2200+
21812201
foreach ($map as $value => $className) {
21822202
$this->addDiscriminatorMapClass($value, $className);
21832203
}

tests/Tests/ORM/Mapping/ClassMetadataTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,14 @@ public function testClassNameMappingDiscriminatorValue(): void
11011101
$xmlElement->children()->{'discriminator-map'}->{'discriminator-mapping'}[0]->attributes()['value'],
11021102
);
11031103
}
1104+
1105+
public function testDiscriminatorMapWithSameClassMultipleTimesDeprecated(): void
1106+
{
1107+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/3519');
1108+
1109+
$cm = new ClassMetadata(CMS\CmsUser::class);
1110+
$cm->setDiscriminatorMap(['foo' => CMS\CmsUser::class, 'bar' => CMS\CmsUser::class]);
1111+
}
11041112
}
11051113

11061114
#[MappedSuperclass]

0 commit comments

Comments
 (0)