1
0
mirror of synced 2024-12-14 15:16:04 +03:00

basic support, need some code refactory and improvements

This commit is contained in:
Fabio B. Silva 2012-01-21 17:39:32 -02:00
parent ed89695a8c
commit 0fbb78e61a
4 changed files with 61 additions and 4 deletions

View File

@ -234,6 +234,18 @@ abstract class AbstractHydrator
// maybe from an additional column that has not been defined in a NativeQuery ResultSetMapping.
continue 2;
}
if (isset ($this->_rsm->newObjectMappings[$key])) {
$cache[$key]['isNewObjectParameter'] = true;
}
}
if (isset ($cache[$key]['isNewObjectParameter'])) {
$argIndex = $this->_rsm->newObjectMappings[$key]['argIndex'];
$objIndex = $this->_rsm->newObjectMappings[$key]['objIndex'];
$className = $this->_rsm->newObjectMappings[$key]['className'];
$rowData['newObjects'][$objIndex]['className'] = $className;
$rowData['newObjects'][$objIndex]['args'][$argIndex] = $cache[$key]['fieldName'];
}
if (isset($cache[$key]['isScalar'])) {

View File

@ -330,6 +330,17 @@ class ObjectHydrator extends AbstractHydrator
}
}
// 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;
}
}
// Hydrate the data chunks
foreach ($rowData as $dqlAlias => $data) {
$entityName = $this->_rsm->aliasMap[$dqlAlias];
@ -531,6 +542,24 @@ class ObjectHydrator extends AbstractHydrator
$result[$resultKey][$name] = $value;
}
}
// Append new object to mixed result sets
if (isset($newObjects)) {
if ( ! isset($resultKey) ) {
$resultKey = $this->_resultCounter - 1;
}
foreach ($newObjects as $newObject) {
$args = array();
$className = $newObject['className'];
foreach ($newObject['args'] as $index => $name) {
$args[$index] = $result[$resultKey][$name];
}
$class = new \ReflectionClass($className);
$result[$resultKey] = $class->newInstanceArgs($args);
}
}
}
/**

View File

@ -77,6 +77,13 @@ class SqlWalker implements TreeWalker
*/
private $sqlParamIndex = 0;
/**
* Counters for generating indexes.
*
* @var integer
*/
private $newObjectCounter;
/**
* @var ParserResult
*/
@ -1225,10 +1232,19 @@ class SqlWalker implements TreeWalker
$sqlSelectExpressions = array();
$this->_rsm->newObjectMappings['className'] = $expr->className;
foreach ($expr->args as $e) {
$resultAliasMap = $this->scalarResultAliasMap;
$sqlSelectExpressions = array();
$objIndex = $this->newObjectCounter ++;
foreach ($expr->args as $key => $e) {
$resultAliasMap = $this->scalarResultAliasMap;
$sqlSelectExpressions[] = $this->walkSelectExpression($e);
$this->_rsm->newObjectMappings['resultAliasMap'][] = array_diff($this->scalarResultAliasMap, $resultAliasMap);
$scalarResultAliasMap = array_diff($this->scalarResultAliasMap, $resultAliasMap);
foreach ($scalarResultAliasMap as $aliasMap) {
$this->_rsm->newObjectMappings[$aliasMap] = array(
'className' => $expr->className,
'objIndex' => $objIndex,
'argIndex' => $key
);
}
}
$sql .= implode(', ', $sqlSelectExpressions);

View File

@ -833,7 +833,7 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$query = $this->_em->createQuery("SELECT new Doctrine\Tests\Models\CMS\CmsUserDTO(u.name, e.email, a.city) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e JOIN u.address a ORDER BY u.name");
$result = $query->getResult();
$this->assertEquals(3, count($result));
$this->assertCount(3, $result);
$this->markTestIncomplete();