1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[DDC-161] Adding ability to use custom hydrators with queries.

This commit is contained in:
Jonathan H. Wage 2010-06-02 23:25:09 -04:00
parent 42e83c4de6
commit 5b148c7b20
4 changed files with 57 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}

View 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);
}
}