Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-3519] Deprecate passing the same class with different discriminator values. #9760

Merged
merged 1 commit into from
Nov 18, 2024
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
9 changes: 7 additions & 2 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Upgrade to 3.4

Using the same class several times in a discriminator map is deprecated.
In 4.0, this will be an error.

# Upgrade to 3.3

## Deprecate `DatabaseDriver`
Expand Down Expand Up @@ -737,7 +742,7 @@ Use `toIterable()` instead.

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

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

## PARTIAL DQL syntax is undeprecated
## PARTIAL DQL syntax is undeprecated

Use of the PARTIAL keyword is not deprecated anymore in DQL, because we will be
able to support PARTIAL objects with PHP 8.4 Lazy Objects and
Expand Down
20 changes: 20 additions & 0 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
use Stringable;

use function array_column;
use function array_count_values;
use function array_diff;
use function array_filter;
use function array_flip;
use function array_intersect;
use function array_key_exists;
use function array_keys;
Expand All @@ -39,6 +42,7 @@
use function defined;
use function enum_exists;
use function explode;
use function implode;
use function in_array;
use function interface_exists;
use function is_string;
Expand Down Expand Up @@ -2178,6 +2182,22 @@ final public function getDiscriminatorColumn(): DiscriminatorColumnMapping
*/
public function setDiscriminatorMap(array $map): void
{
if (count(array_flip($map)) !== count($map)) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/3519',
<<<'DEPRECATION'
Mapping a class to multiple discriminator values is deprecated,
and the discriminator mapping of %s contains duplicate values
for the following discriminator values: %s.
DEPRECATION,
$this->name,
implode(', ', array_keys(array_filter(array_count_values($map), static function (int $value): bool {
return $value > 1;
}))),
);
}

foreach ($map as $value => $className) {
$this->addDiscriminatorMapClass($value, $className);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,14 @@ public function testClassNameMappingDiscriminatorValue(): void
$xmlElement->children()->{'discriminator-map'}->{'discriminator-mapping'}[0]->attributes()['value'],
);
}

public function testDiscriminatorMapWithSameClassMultipleTimesDeprecated(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/3519');

$cm = new ClassMetadata(CMS\CmsUser::class);
$cm->setDiscriminatorMap(['foo' => CMS\CmsUser::class, 'bar' => CMS\CmsUser::class]);
}
}

#[MappedSuperclass]
Expand Down
Loading