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

Add support for configuring the identity generation preferences #1813

Merged
merged 1 commit into from
Aug 5, 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: 9 additions & 0 deletions config/schema/doctrine-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
<xsd:element name="entity-listeners" type="entity_listeners" minOccurs="0" maxOccurs="1" />
<xsd:element name="second-level-cache" type="second-level-cache" minOccurs="0" maxOccurs="1" />
<xsd:element name="schema-ignore-class" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="identity-generation-preference" type="identity_generation_preference" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
</xsd:group>

Expand Down Expand Up @@ -282,4 +283,12 @@
<xsd:element name="datetime-function" type="named_scalar" />
</xsd:choice>
</xsd:complexType>

<xsd:complexType name="identity_generation_preference">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="platform" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
18 changes: 17 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
17 changes: 17 additions & 0 deletions tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" ?>

<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">

<config>
<dbal default-connection="default">
<connection name="default" dbname="db" />
</dbal>

<orm default-entity-manager="em1">
<entity-manager name="em1">
<mapping name="YamlBundle" />
<identity-generation-preference platform="Doctrine\DBAL\Platforms\PostgreSQLPlatform">identity</identity-generation-preference>
</entity-manager>
<entity-manager name="em2">
<identity-generation-preference platform="Doctrine\DBAL\Platforms\PostgreSQLPlatform">sequence</identity-generation-preference>
<mapping name="YamlBundle" />
</entity-manager>
</orm>
</config>
</srv:container>
Original file line number Diff line number Diff line change
@@ -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
Loading