diff --git a/UPGRADE.md b/UPGRADE.md index 9e63cab54f6..fba8e47aa2b 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -395,6 +395,11 @@ Use `toIterable()` instead. # Upgrade to 2.13 +## Deprecated using the `IDENTITY` identifier strategy on platform that do not support identity columns + +If identity columns are emulated with sequences on the platform you are using, +you should switch to the `SEQUENCE` strategy. + ## Deprecated passing `null` to `Doctrine\ORM\Query::setFirstResult()` `$query->setFirstResult(null);` is equivalent to `$query->setFirstResult(0)`. diff --git a/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php b/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php index 56af5b4dab4..7a76286d898 100644 --- a/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php +++ b/lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM\Id; +use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\EntityManagerInterface; /** @@ -21,6 +22,14 @@ class BigIntegerIdentityGenerator extends AbstractIdGenerator public function __construct( private ?string $sequenceName = null ) { + if ($sequenceName !== null) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8850', + 'Passing a sequence name to the IdentityGenerator is deprecated in favor of using %s. $sequenceName will be removed in ORM 3.0', + SequenceGenerator::class + ); + } } public function generateId(EntityManagerInterface $em, ?object $entity): string diff --git a/lib/Doctrine/ORM/Id/IdentityGenerator.php b/lib/Doctrine/ORM/Id/IdentityGenerator.php index af55dda6bc1..aca008b8049 100644 --- a/lib/Doctrine/ORM/Id/IdentityGenerator.php +++ b/lib/Doctrine/ORM/Id/IdentityGenerator.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM\Id; +use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\EntityManagerInterface; /** @@ -21,6 +22,14 @@ class IdentityGenerator extends AbstractIdGenerator public function __construct( private ?string $sequenceName = null ) { + if ($sequenceName !== null) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8850', + 'Passing a sequence name to the IdentityGenerator is deprecated in favor of using %s. $sequenceName will be removed in ORM 3.0', + SequenceGenerator::class + ); + } } public function generateId(EntityManagerInterface $em, ?object $entity): int diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 17077acdd88..aaf4eecde71 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -32,6 +32,7 @@ use function count; use function end; use function explode; +use function get_class; use function in_array; use function is_subclass_of; use function str_contains; @@ -492,6 +493,18 @@ private function completeIdGeneratorMapping(ClassMetadata $class): void // Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour. if ($this->getTargetPlatform()->usesSequenceEmulatedIdentityColumns()) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8850', + <<<'DEPRECATION' +Context: Loading metadata for class %s +Problem: Using the IDENTITY generator strategy with platform "%s" is deprecated and will not be possible in Doctrine ORM 3.0. +Solution: Use the SEQUENCE generator strategy instead. +DEPRECATION + , + $class->name, + get_class($this->getTargetPlatform()) + ); $columnName = $class->getSingleIdentifierColumnName(); $quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']); $sequencePrefix = $class->getSequencePrefix($this->getTargetPlatform()); diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index e0101867488..c496f403e71 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -96,6 +96,27 @@ public function testGetMetadataForSingleClass(): void self::assertTrue($cmMap1->hasField('name')); } + public function testUsingIdentityWithAPlatformThatDoesNotSupportIdentityColumnsIsDeprecated(): void + { + $cm = $this->createValidClassMetadata(); + $cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); + $cmf = new ClassMetadataFactoryTestSubject(); + $driver = $this->createMock(Driver::class); + $platform = $this->createStub(AbstractPlatform::class); + $platform->method('usesSequenceEmulatedIdentityColumns')->willReturn(true); + $platform->method('getIdentitySequenceName')->willReturn('whatever'); + $driver->method('getDatabasePlatform')->willReturn($platform); + $entityManager = $this->createEntityManager( + new MetadataDriverMock(), + new Connection([], $driver) + ); + $cmf->setEntityManager($entityManager); + $cmf->setMetadataForClass($cm->name, $cm); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8850'); + $cmf->getMetadataFor($cm->name); + } + public function testItThrowsWhenUsingAutoWithIncompatiblePlatform(): void { $cm1 = $this->createValidClassMetadata(); diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 9a2c61ecd5c..d36f1b9d06b 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -1074,7 +1074,7 @@ protected function dropTableIfExists(string $name): void protected function dropAndCreateTable(Table $table): void { $schemaManager = $this->createSchemaManager(); - $platform = $schemaManager->getDatabasePlatform(); + $platform = $this->_em->getConnection()->getDatabasePlatform(); $tableName = $table->getQuotedName($platform); $this->dropTableIfExists($tableName);