diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index b6fa3a99e..102ab90df 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -462,4 +462,28 @@ class Configuration extends \Doctrine\DBAL\Configuration { $this->_attributes['customDatetimeFunctions'] = array_change_key_case($functions); } + + /** + * Get a custom hydrator class name if it exists or return null if it + * does not. + * + * @param string $name + * @return string $className + */ + public function getHydrator($name) + { + return isset($this->_attributes['customHydrators'][$name]) ? + $this->_attributes['customHydrators'][$name] : null; + } + + /** + * Add a hydrator class associated with a hydration mode name. + * + * @param string $name The name of the hydration mode. + * @param string $class The class name associated with the name. + */ + public function addHydrator($name, $class) + { + $this->_attributes['customHydrators'][$name] = $class; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0ff16f640..660e10ba9 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -616,6 +616,10 @@ class EntityManager $hydrator = new Internal\Hydration\SingleScalarHydrator($this); break; default: + if ($class = $this->_config->getHydrator($hydrationMode)) { + $hydrator = new $class($this); + break; + } throw ORMException::invalidHydrationMode($hydrationMode); } diff --git a/tests/Doctrine/Tests/ORM/Hydration/AllTests.php b/tests/Doctrine/Tests/ORM/Hydration/AllTests.php index 76a9cbcb1..d2ecafec3 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Hydration/AllTests.php @@ -25,6 +25,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Hydration\ScalarHydratorTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Hydration\SingleScalarHydratorTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Hydration\ResultSetMappingTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Hydration\CustomHydratorTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Hydration/CustomHydratorTest.php b/tests/Doctrine/Tests/ORM/Hydration/CustomHydratorTest.php new file mode 100644 index 000000000..a7ef599ea --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Hydration/CustomHydratorTest.php @@ -0,0 +1,28 @@ +_getTestEntityManager(); + $config = $em->getConfiguration(); + $config->addHydrator('CustomHydrator', 'Doctrine\Tests\ORM\Hydration\CustomHydrator'); + + $hydrator = $em->newHydrator('CustomHydrator'); + $this->assertTrue($hydrator instanceof \Doctrine\Tests\ORM\Hydration\CustomHydrator); + } +} + +class CustomHydrator extends AbstractHydrator +{ + protected function _hydrateAll() + { + return $this->_stmt->fetchAll(PDO::FETCH_ASSOC); + } +} \ No newline at end of file