Sfoglia il codice sorgente

[DoctrineBundle] made it easier to configure Doctrine DBAL when only one connection needs to be configured

Fabien Potencier 15 anni fa
parent
commit
c9211d9300

+ 17 - 13
src/Symfony/Framework/DoctrineBundle/DependencyInjection/DoctrineExtension.php

@@ -57,26 +57,33 @@ class DoctrineExtension extends LoaderExtension
     $configuration->merge($loader->load($this->resources['dbal']));
 
     $defaultConnection = array(
-      'driver'              => 'PDOSqlite',
-      'dbname'              => 'symfony',
+      'driver'              => 'PDOMySQL',
       'user'                => 'root',
       'password'            => null,
       'host'                => 'localhost',
       'port'                => null,
-      'path'                => '%kernel.root_dir%/symfony.sqlite',
       'event_manager_class' => 'Doctrine\Common\EventManager',
       'configuration_class' => 'Doctrine\DBAL\Configuration',
       'wrapper_class'       => null,
       'options'             => array()
     );
 
-    $config['default_connection'] = isset($config['default_connection']) ?
-      $config['default_connection'] : 'default';
+    $config['default_connection'] = isset($config['default_connection']) ? $config['default_connection'] : 'default';
 
-    $config['connections'] = isset($config['connections']) ?
-      $config['connections'] : array($config['default_connection'] => $defaultConnection
-    );
-    foreach ($config['connections'] as $name => $connection)
+    $connections = array();
+    if (isset($config['connections']))
+    {
+      foreach ($config['connections'] as $name => $connection)
+      {
+        $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection;
+      }
+    }
+    else
+    {
+      $connections = array($config['default_connection'] => $config);
+    }
+
+    foreach ($connections as $name => $connection)
     {
       $connection = array_merge($defaultConnection, $connection);
       $configurationClass = isset($connection['configuration_class']) ?
@@ -153,10 +160,7 @@ class DoctrineExtension extends LoaderExtension
     $loader = new XmlFileLoader(__DIR__.'/../Resources/config');
     $configuration->merge($loader->load($this->resources['orm']));
 
-    $config['default_entity_manager'] = isset($config['default_entity_manager']) ?
-      $config['default_entity_manager'] : 
-      'default'
-    ;
+    $config['default_entity_manager'] = isset($config['default_entity_manager']) ? $config['default_entity_manager'] : 'default';
     foreach (array('metadata_driver', 'cache_driver') as $key)
     {
       if (isset($config[$key]))

+ 23 - 0
src/Symfony/Framework/DoctrineBundle/Resources/config/schema/dic/doctrine/doctrine-1.0.xsd

@@ -8,6 +8,11 @@
   <xsd:element name="dbal" type="dbal" />
 
   <xsd:complexType name="dbal">
+    <xsd:sequence>
+      <xsd:element name="connections" type="connections" minOccurs="0" maxOccurs="1" />
+    </xsd:sequence>
+
+    <xsd:attribute name="default_connection" type="xsd:string" />
     <xsd:attribute name="dbname" type="xsd:string" />
     <xsd:attribute name="host" type="xsd:string" />
     <xsd:attribute name="port" type="xsd:integer" />
@@ -17,4 +22,22 @@
     <xsd:attribute name="options" type="xsd:string" />
     <xsd:attribute name="path" type="xsd:string" />
   </xsd:complexType>
+
+  <xsd:complexType name="connections">
+    <xsd:sequence>
+      <xsd:element name="connection" type="connection" minOccurs="1" maxOccurs="unbounded" />
+    </xsd:sequence>
+  </xsd:complexType>
+
+  <xsd:complexType name="connection">
+    <xsd:attribute name="id" type="xsd:string" use="required" />
+    <xsd:attribute name="dbname" type="xsd:string" use="required" />
+    <xsd:attribute name="host" type="xsd:string" />
+    <xsd:attribute name="port" type="xsd:integer" />
+    <xsd:attribute name="user" type="xsd:string" />
+    <xsd:attribute name="password" type="xsd:string" />
+    <xsd:attribute name="driver" type="xsd:string" />
+    <xsd:attribute name="options" type="xsd:string" />
+    <xsd:attribute name="path" type="xsd:string" />
+  </xsd:complexType>
 </xsd:schema>