From e63c2e2315d749064712a30768f8b85c855c41b2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 27 Apr 2011 12:01:28 +0200 Subject: [PATCH] [DoctrineBundle] added back the possibility to easily define one DBAL connection The reasonning reads as follows: Most users will only ever use one database connection (and probably one entity manager for that matter). So, this most common use case should be as easy as possible to configure. This is BC. Before: doctrine: dbal: default_connection: default connections: default: driver: pdo_mysql dbname: Symfony2 user: root password: null After (optional): doctrine: dbal: driver: pdo_mysql dbname: Symfony2 user: root password: null --- .../DependencyInjection/Configuration.php | 16 ++++++++++++++++ .../xml/dbal_service_single_connection.xml | 2 +- .../yml/dbal_service_single_connection.yml | 11 ++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php index 6b15c3d25ad7..5fa370a03206 100644 --- a/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php @@ -58,6 +58,22 @@ private function addDbalSection(ArrayNodeDefinition $node) $node ->children() ->arrayNode('dbal') + ->beforeNormalization() + ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v) && !array_key_exists('connection', $v); }) + ->then(function ($v) { + $connection = array(); + foreach (array('dbname', 'host', 'port', 'user', 'password', 'driver', 'driver_class', 'options', 'path', 'memory', 'unix_socket', 'wrapper_class', 'platform_service', 'charset', 'logging') as $key) { + if (array_key_exists($key, $v)) { + $connection[$key] = $v[$key]; + unset($v[$key]); + } + } + $v['default_connection'] = isset($v['default_connection']) ? (string) $v['default_connection'] : 'default'; + $v['connections'] = array($v['default_connection'] => $connection); + + return $v; + }) + ->end() ->children() ->scalarNode('default_connection')->end() ->end() diff --git a/src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/config/xml/dbal_service_single_connection.xml b/src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/config/xml/dbal_service_single_connection.xml index fa6392e192ba..a8ec0bd79f7a 100644 --- a/src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/config/xml/dbal_service_single_connection.xml +++ b/src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/config/xml/dbal_service_single_connection.xml @@ -7,7 +7,7 @@ http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> - +