Skip to content

[DoctrineBundle] Added custom hydrator for the EntityManager from configuration #471

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

Merged
merged 4 commits into from
Apr 4, 2011
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
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ private function getOrmEntityManagersNode()
->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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
$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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,21 @@ public function testSetCustomFunctions()
$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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
doctrine:
dbal: ~
orm:
entity_managers:
default:
hydrators:
test_hydrator: Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator
mappings:
Yaml: ~
Original file line number Diff line number Diff line change
@@ -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();
}
}