ソースを参照

Merge remote branch 'francisbesset/doctrine_orm_custom_hydrator'

* francisbesset/doctrine_orm_custom_hydrator:
  [DoctrineBundle] Added tests for add custom hydration mode
  Updated XSD schema
  Moved hydrators configuration in entity manager
  [DoctrineBundle] Added custom hydrator for the EntityManager from configuration
Fabien Potencier 14 年 前
コミット
b1e0403c96

+ 12 - 0
src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php

@@ -156,6 +156,18 @@ class Configuration
                     ->scalarNode('connection')->end()
                     ->scalarNode('class_metadata_factory_name')->defaultValue('%doctrine.orm.class_metadata_factory_name%')->end()
                 ->end()
+                ->fixXmlConfig('hydrator')
+                ->children()
+                    ->arrayNode('hydrators')
+                        ->useAttributeAsKey('name')
+                        ->prototype('scalar')
+                            ->beforeNormalization()
+                                ->ifTrue(function($v) { return is_array($v) && isset($v['class']); })
+                                ->then(function($v) { return $v['class']; })
+                            ->end()
+                        ->end()
+                    ->end()
+                ->end()
                 ->fixXmlConfig('mapping')
                 ->children()
                     ->arrayNode('mappings')

+ 4 - 0
src/Symfony/Bundle/DoctrineBundle/DependencyInjection/DoctrineExtension.php

@@ -197,6 +197,10 @@ class DoctrineExtension extends AbstractDoctrineExtension
             $ormConfigDef->addMethodCall($method, array($arg));
         }
 
+        foreach ($entityManager['hydrators'] as $name => $class) {
+            $ormConfigDef->addMethodCall('addCustomHydrationMode', array ($name, $class));
+        }
+
         if (!empty($entityManager['dql'])) {
             foreach ($entityManager['dql']['string_functions'] as $name => $function) {
                 $ormConfigDef->addMethodCall('addCustomStringFunction', array ($name, $function));

+ 1 - 0
src/Symfony/Bundle/DoctrineBundle/Resources/config/schema/doctrine-1.0.xsd

@@ -93,6 +93,7 @@
             <xsd:element name="mapping" type="mapping" />
             <xsd:element name="metadata-cache-driver" type="metadata_cache_driver" minOccurs="0" maxOccurs="1" />
             <xsd:element name="dql" type="dql" minOccurs="0" maxOccurs="1" />
+            <xsd:element name="hydrator" type="type" minOccurs="0" maxOccurs="unbounded" />
         </xsd:choice>
 
         <xsd:attribute name="connection" type="xsd:string" />

+ 15 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php

@@ -669,6 +669,21 @@ abstract class AbstractDoctrineExtensionTest extends TestCase
         $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomDatetimeFunction', array('test_datetime', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestDatetimeFunction'));
     }
 
+    public function testAddCustomHydrationMode()
+    {
+        $container = $this->getContainer(array('YamlBundle'));
+
+        $loader = new DoctrineExtension();
+        $container->registerExtension($loader);
+        $this->loadFromFile($container, 'orm_hydration_mode');
+        $container->getCompilerPassConfig()->setOptimizationPasses(array());
+        $container->getCompilerPassConfig()->setRemovingPasses(array());
+        $container->compile();
+
+        $definition = $container->getDefinition('doctrine.orm.default_configuration');
+        $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomHydrationMode', array('test_hydrator', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator'));
+    }
+
     protected function getContainer($bundles = 'YamlBundle', $vendor = null)
     {
         if (!is_array($bundles)) {

+ 18 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/config/xml/orm_hydration_mode.xml

@@ -0,0 +1,18 @@
+<?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 />
+        <orm default-entity-manager="default">
+            <entity-manager name="default">
+                <hydrator name="test_hydrator" class="Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator" />
+                <mapping name="Yaml" />
+            </entity-manager>
+        </orm>
+    </config>
+</srv:container>

+ 9 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/config/yml/orm_hydration_mode.yml

@@ -0,0 +1,9 @@
+doctrine:
+    dbal: ~
+    orm:
+        entity_managers:
+            default:
+                hydrators:
+                    test_hydrator: Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator
+                mappings:
+                    Yaml: ~

+ 20 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/TestHydrator.php

@@ -0,0 +1,20 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection;
+
+class TestHydrator extends \Doctrine\ORM\Internal\Hydration\AbstractHydrator
+{
+    protected function _hydrateAll();
+    {
+        return array();
+    }
+}