From 9926a4a4dc47c95ca0dca03a5140b540be36d706 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 5 Aug 2024 12:02:33 +0200 Subject: [PATCH] Add support for configuring the identity generation preferences --- config/schema/doctrine-1.0.xsd | 9 +++++++ src/DependencyInjection/Configuration.php | 18 ++++++++++++- src/DependencyInjection/DoctrineExtension.php | 1 + .../AbstractDoctrineExtensionTest.php | 17 +++++++++++++ .../orm_identity_generation_preferences.xml | 25 +++++++++++++++++++ .../orm_identity_generation_preferences.yml | 20 +++++++++++++++ 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/DependencyInjection/Fixtures/config/xml/orm_identity_generation_preferences.xml create mode 100644 tests/DependencyInjection/Fixtures/config/yml/orm_identity_generation_preferences.yml diff --git a/config/schema/doctrine-1.0.xsd b/config/schema/doctrine-1.0.xsd index a3f549b0..8e0a2bea 100644 --- a/config/schema/doctrine-1.0.xsd +++ b/config/schema/doctrine-1.0.xsd @@ -218,6 +218,7 @@ + @@ -282,4 +283,12 @@ + + + + + + + + diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8841aed7..06cf3dbd 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -5,6 +5,7 @@ use Doctrine\DBAL\Schema\LegacySchemaManagerFactory; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Mapping\Driver\AnnotationDriver; use Doctrine\ORM\Proxy\ProxyFactory; @@ -804,13 +805,28 @@ private function getOrmEntityManagersNode(): ArrayNodeDefinition ->end() ->end() ->end() + ->fixXmlConfig('identity_generation_preference') + ->children() + ->arrayNode('identity_generation_preferences') + ->info('Configures the preferences for identity generation when using the AUTO strategy. Valid values are "SEQUENCE" or "IDENTITY".') + ->useAttributeAsKey('platform') + ->prototype('scalar') + ->beforeNormalization() + ->ifString() + ->then(static function ($v) { + return constant(ClassMetadata::class . '::GENERATOR_TYPE_' . strtoupper($v)); + }) + ->end() + ->end() + ->end() + ->end() ->end(); return $node; } /** - * Return a ORM cache driver node for an given entity manager + * Return an ORM cache driver node for a given entity manager */ private function getOrmCacheDriverNode(string $name): ArrayNodeDefinition { diff --git a/src/DependencyInjection/DoctrineExtension.php b/src/DependencyInjection/DoctrineExtension.php index 26c8c9af..30cbfa6f 100644 --- a/src/DependencyInjection/DoctrineExtension.php +++ b/src/DependencyInjection/DoctrineExtension.php @@ -719,6 +719,7 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $ 'setTypedFieldMapper' => new Reference($entityManager['typed_field_mapper']), 'setEntityListenerResolver' => new Reference(sprintf('doctrine.orm.%s_entity_listener_resolver', $entityManager['name'])), 'setLazyGhostObjectEnabled' => '%doctrine.orm.enable_lazy_ghost_objects%', + 'setIdentityGenerationPreferences' => $entityManager['identity_generation_preferences'], ]; if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) { diff --git a/tests/DependencyInjection/AbstractDoctrineExtensionTest.php b/tests/DependencyInjection/AbstractDoctrineExtensionTest.php index a9a515cf..4c91dc2f 100644 --- a/tests/DependencyInjection/AbstractDoctrineExtensionTest.php +++ b/tests/DependencyInjection/AbstractDoctrineExtensionTest.php @@ -13,10 +13,12 @@ use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Schema\LegacySchemaManagerFactory; use Doctrine\ORM\Configuration as OrmConfiguration; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\Driver\AnnotationDriver; use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver; use Doctrine\ORM\Proxy\ProxyFactory; @@ -714,6 +716,21 @@ public function testSetNamingStrategy(): void $this->assertDICDefinitionMethodCallOnce($def2, 'setNamingStrategy', [0 => new Reference('doctrine.orm.naming_strategy.underscore')]); } + public function testSetIdentityGenerationPreferences(): void + { + if (! interface_exists(EntityManagerInterface::class)) { + self::markTestSkipped('This test requires ORM'); + } + + $container = $this->loadContainer('orm_identity_generation_preferences'); + + $def1 = $container->getDefinition('doctrine.orm.em1_configuration'); + $def2 = $container->getDefinition('doctrine.orm.em2_configuration'); + + $this->assertDICDefinitionMethodCallOnce($def1, 'setIdentityGenerationPreferences', [0 => [PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY]]); + $this->assertDICDefinitionMethodCallOnce($def2, 'setIdentityGenerationPreferences', [0 => [PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE]]); + } + public function testSetQuoteStrategy(): void { if (! interface_exists(EntityManagerInterface::class)) { diff --git a/tests/DependencyInjection/Fixtures/config/xml/orm_identity_generation_preferences.xml b/tests/DependencyInjection/Fixtures/config/xml/orm_identity_generation_preferences.xml new file mode 100644 index 00000000..1318a9d5 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/config/xml/orm_identity_generation_preferences.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + identity + + + sequence + + + + + diff --git a/tests/DependencyInjection/Fixtures/config/yml/orm_identity_generation_preferences.yml b/tests/DependencyInjection/Fixtures/config/yml/orm_identity_generation_preferences.yml new file mode 100644 index 00000000..c0e62f0e --- /dev/null +++ b/tests/DependencyInjection/Fixtures/config/yml/orm_identity_generation_preferences.yml @@ -0,0 +1,20 @@ +doctrine: + dbal: + default_connection: default + connections: + default: + dbname: db + + orm: + default_entity_manager: em1 + entity_managers: + em1: + mappings: + YamlBundle: ~ + identity_generation_preferences: + Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity + em2: + mappings: + YamlBundle: ~ + identity_generation_preferences: + Doctrine\DBAL\Platforms\PostgreSQLPlatform: sequence