Skip to content

Commit

Permalink
DDC-1385 - Fixed Mixed result hydration using INDEX BY to work on the…
Browse files Browse the repository at this point in the history
… top-level indexes, not some weird result. This is a BC break to those that actually use this crazy logic, sorry for that :-)
  • Loading branch information
beberlei committed Oct 16, 2011
1 parent fc73845 commit cb0e5db
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
22 changes: 12 additions & 10 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,19 +430,16 @@ protected function _hydrateRow(array $data, array &$cache, array &$result)
$element = $this->_getEntity($rowData[$dqlAlias], $dqlAlias);
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
$field = $this->_rsm->indexByMap[$dqlAlias];
$key = $this->_ce[$entityName]->reflFields[$field]->getValue($element);
$resultKey = $this->_ce[$entityName]->reflFields[$field]->getValue($element);
if ($this->_rsm->isMixed) {
$element = array($key => $element);
$result[] = $element;
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $this->_resultCounter;
++$this->_resultCounter;
} else {
$result[$key] = $element;
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $key;
$element = array(0 => $element);
}

$result[$resultKey] = $element;
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey;

if (isset($this->_hints['collection'])) {
$this->_hints['collection']->hydrateSet($key, $element);
$this->_hints['collection']->hydrateSet($resultKey, $element);
}
} else {
if ($this->_rsm->isMixed) {
Expand All @@ -464,6 +461,7 @@ protected function _hydrateRow(array $data, array &$cache, array &$result)
// Update result pointer
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
$this->_resultPointers[$dqlAlias] = $result[$index];
$resultKey = $index;
/*if ($this->_rsm->isMixed) {
$result[] = $result[$index];
++$this->_resultCounter;
Expand All @@ -474,8 +472,12 @@ protected function _hydrateRow(array $data, array &$cache, array &$result)

// Append scalar values to mixed result sets
if (isset($scalars)) {
if ( ! isset($resultKey) ) {
$resultKey = $this->_resultCounter - 1;
}

foreach ($scalars as $name => $value) {
$result[$this->_resultCounter - 1][$name] = $value;
$result[$resultKey][$name] = $value;
}
}
}
Expand Down
69 changes: 58 additions & 11 deletions tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,24 +352,24 @@ public function testMixedQueryFetchJoinCustomIndex()

$this->assertEquals(2, count($result));
$this->assertTrue(is_array($result));
$this->assertTrue(is_array($result[0]));
$this->assertTrue(is_array($result[1]));
$this->assertTrue(is_array($result[2]));

// test the scalar values
$this->assertEquals('ROMANB', $result[0]['nameUpper']);
$this->assertEquals('JWAGE', $result[1]['nameUpper']);
$this->assertEquals('ROMANB', $result[1]['nameUpper']);
$this->assertEquals('JWAGE', $result[2]['nameUpper']);

$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]['1']);
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[1]['2']);
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $result[0]['1']->phonenumbers);
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[1][0]);
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[2][0]);
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $result[1][0]->phonenumbers);
// first user => 2 phonenumbers. notice the custom indexing by user id
$this->assertEquals(2, count($result[0]['1']->phonenumbers));
$this->assertEquals(2, count($result[1][0]->phonenumbers));
// second user => 1 phonenumber. notice the custom indexing by user id
$this->assertEquals(1, count($result[1]['2']->phonenumbers));
$this->assertEquals(1, count($result[2][0]->phonenumbers));
// test the custom indexing of the phonenumbers
$this->assertTrue(isset($result[0]['1']->phonenumbers['42']));
$this->assertTrue(isset($result[0]['1']->phonenumbers['43']));
$this->assertTrue(isset($result[1]['2']->phonenumbers['91']));
$this->assertTrue(isset($result[1][0]->phonenumbers['42']));
$this->assertTrue(isset($result[1][0]->phonenumbers['43']));
$this->assertTrue(isset($result[2][0]->phonenumbers['91']));
}

/**
Expand Down Expand Up @@ -1169,4 +1169,51 @@ public function testMissingIdForSingleValuedChildEntity()
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $result[0][0]->address);
$this->assertNull($result[1][0]->address);
}

/**
* @group DDC-1385
*/
public function testIndexByAndMixedResult()
{
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$rsm->addFieldResult('u', 'u__id', 'id');
$rsm->addFieldResult('u', 'u__status', 'status');
$rsm->addScalarResult('sclr0', 'nameUpper');
$rsm->addIndexBy('u', 'id');

// Faked result set
$resultSet = array(
//row1
array(
'u__id' => '1',
'u__status' => 'developer',
'sclr0' => 'ROMANB',
),
array(
'u__id' => '2',
'u__status' => 'developer',
'sclr0' => 'JWAGE',
),
);

$stmt = new HydratorMockStatement($resultSet);
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

$result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));

$this->assertEquals(2, count($result));
$this->assertTrue(isset($result[1]));
$this->assertEquals(1, $result[1][0]->id);
$this->assertTrue(isset($result[2]));
$this->assertEquals(2, $result[2][0]->id);
}

/**
* @group DDC-1385
*/
public function testIndexByAndScalarResult()
{
$rsm = new ResultSetMapping;
}
}

0 comments on commit cb0e5db

Please sign in to comment.