[DDC-161] Adding ability to use custom hydrators with queries.
This commit is contained in:
parent
42e83c4de6
commit
5b148c7b20
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
28
tests/Doctrine/Tests/ORM/Hydration/CustomHydratorTest.php
Normal file
28
tests/Doctrine/Tests/ORM/Hydration/CustomHydratorTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Hydration;
|
||||
|
||||
use PDO, Doctrine\ORM\Internal\Hydration\AbstractHydrator;
|
||||
|
||||
require_once __DIR__ . '/../../TestInit.php';
|
||||
|
||||
class CustomHydratorTest extends HydrationTestCase
|
||||
{
|
||||
public function testCustomHydrator()
|
||||
{
|
||||
$em = $this->_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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user