From 29de3e00fff0a0e70029c21dc4bc0a40dd7856f8 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Wed, 23 Apr 2014 05:10:23 +0000 Subject: [PATCH] Added support for NEW operator when using ArrayHydration if user desires to benefit from this funcionality. --- .../ORM/Internal/Hydration/ArrayHydrator.php | 34 +++++++++++++++++++ .../Tests/ORM/Functional/NewOperatorTest.php | 17 ++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php index c336e7a5d..8adf2f0ed 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php @@ -120,6 +120,17 @@ class ArrayHydrator extends AbstractHydrator ++$this->_resultCounter; } } + + // Extract "new" object constructor arguments. They're appended at the end. + if (isset($rowData['newObjects'])) { + $newObjects = $rowData['newObjects']; + + unset($rowData['newObjects']); + + if (empty($rowData)) { + ++$this->_resultCounter; + } + } // 2) Now hydrate the data found in the current row. foreach ($rowData as $dqlAlias => $data) { @@ -262,6 +273,29 @@ class ArrayHydrator extends AbstractHydrator $result[$resultKey][$name] = $value; } } + + // Append new object to mixed result sets + if (isset($newObjects)) { + if ( ! isset($resultKey) ) { + $resultKey = $this->_resultCounter - 1; + } + + $count = count($newObjects); + + foreach ($newObjects as $objIndex => $newObject) { + $class = $newObject['class']; + $args = $newObject['args']; + $obj = $class->newInstanceArgs($args); + + if ($count === 1) { + $result[$resultKey] = $obj; + + continue; + } + + $result[$resultKey][$objIndex] = $obj; + } + } } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php b/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php index 98fe0dcb2..63cc89bac 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM\Functional; +use Doctrine\ORM\Query; use Doctrine\Tests\Models\CMS\CmsUser; use Doctrine\Tests\Models\CMS\CmsEmail; use Doctrine\Tests\Models\CMS\CmsAddress; @@ -12,7 +13,6 @@ use Doctrine\Tests\Models\CMS\CmsPhonenumber; */ class NewOperatorTest extends \Doctrine\Tests\OrmFunctionalTestCase { - /** * @var array */ @@ -25,6 +25,14 @@ class NewOperatorTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->loadFixtures(); } + + public function provideDataForHydrationMode() + { + return array( + array(Query::HYDRATE_ARRAY), + array(Query::HYDRATE_OBJECT), + ); + } private function loadFixtures() { @@ -87,7 +95,10 @@ class NewOperatorTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->fixtures = array($u1, $u2, $u3); } - public function testShouldSupportsBasicUsage() + /** + * @dataProvider provideDataForHydrationMode + */ + public function testShouldSupportsBasicUsage($hydrationMode) { $dql = " SELECT @@ -106,7 +117,7 @@ class NewOperatorTest extends \Doctrine\Tests\OrmFunctionalTestCase u.name"; $query = $this->_em->createQuery($dql); - $result = $query->getResult(); + $result = $query->getResult($hydrationMode); $this->assertCount(3, $result);