瀏覽代碼

[DoctrineBundle] added an auto-mapping option to let Symfony register all enabled bundle mappings

Most of the time, you just want to register all your bundle mappings. It's a bit
tedious to do it by hand, not because of the amount of configuration you need to
type, but mainly because you can easily forget to do so
(also see https://github.com/symfony/symfony/pull/502).

So, setting auto_mapping to true allows Symfony to automatically register the mappings
it founds in the enabled bundles (default is false).

Even if auto_mapping is true, you can still define your mappings to add some more
or to override the defaults.

This change means that the default configuration that works most of the time for
most people is simple:

orm:
  auto_mapping: true
Fabien Potencier 14 年之前
父節點
當前提交
dc85727b5a

+ 9 - 0
src/Symfony/Bundle/DoctrineAbstractBundle/DependencyInjection/AbstractDoctrineExtension.php

@@ -42,6 +42,15 @@ abstract class AbstractDoctrineExtension extends Extension
      */
     protected function loadMappingInformation(array $objectManager, ContainerBuilder $container)
     {
+        if (!$objectManager['mappings'] && $objectManager['auto_mapping']) {
+            // automatically register bundle mappings
+            foreach (array_keys($container->getParameter('kernel.bundles')) as $bundle) {
+                if (!isset($objectManager['mappings'][$bundle])) {
+                    $objectManager['mappings'][$bundle] = null;
+                }
+            }
+        }
+
         foreach ($objectManager['mappings'] as $mappingName => $mappingConfig) {
             $mappingConfig = array_replace(array(
                 'dir'    => false,

+ 4 - 3
src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php

@@ -156,13 +156,15 @@ class Configuration implements ConfigurationInterface
             ->children()
                 ->arrayNode('orm')
                     ->beforeNormalization()
-                        ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('entity_managers', $v) && !array_key_exists('entity_manager', $v); })
+                        ->ifTrue(function ($v) { return null === $v || (is_array($v) && !array_key_exists('entity_managers', $v) && !array_key_exists('entity_manager', $v)); })
                         ->then(function ($v) {
+                            $v = (array) $v;
                             $entityManager = array();
                             foreach (array(
                                 'result_cache_driver', 'result-cache-driver',
                                 'metadata_cache_driver', 'metadata-cache-driver',
                                 'query_cache_driver', 'query-cache-driver',
+                                'auto_mapping', 'auto-mapping',
                                 'mappings', 'mapping',
                                 'connection'
                             ) as $key) {
@@ -206,6 +208,7 @@ class Configuration implements ConfigurationInterface
                 ->children()
                     ->scalarNode('connection')->end()
                     ->scalarNode('class_metadata_factory_name')->defaultValue('%doctrine.orm.class_metadata_factory_name%')->end()
+                    ->scalarNode('auto_mapping')->defaultFalse()->end()
                 ->end()
                 ->fixXmlConfig('hydrator')
                 ->children()
@@ -217,8 +220,6 @@ class Configuration implements ConfigurationInterface
                 ->fixXmlConfig('mapping')
                 ->children()
                     ->arrayNode('mappings')
-                        ->isRequired()
-                        ->requiresAtLeastOneElement()
                         ->useAttributeAsKey('name')
                         ->prototype('array')
                             ->beforeNormalization()