Commit c500f710 authored by zYne's avatar zYne

--no commit message

--no commit message
parent f99e3dd4
...@@ -145,6 +145,10 @@ class Doctrine_Hydrate2 extends Doctrine_Access ...@@ -145,6 +145,10 @@ class Doctrine_Hydrate2 extends Doctrine_Access
{ {
return $this->tableAliases; return $this->tableAliases;
} }
public function setTableAliases(array $aliases)
{
$this->tableAliases = $aliases;
}
/** /**
* copyAliases * copyAliases
* *
...@@ -299,7 +303,6 @@ class Doctrine_Hydrate2 extends Doctrine_Access ...@@ -299,7 +303,6 @@ class Doctrine_Hydrate2 extends Doctrine_Access
} }
$stmt = $this->conn->execute($query, $params); $stmt = $this->conn->execute($query, $params);
return $this->parseData($stmt); return $this->parseData($stmt);
} }
...@@ -311,6 +314,31 @@ class Doctrine_Hydrate2 extends Doctrine_Access ...@@ -311,6 +314,31 @@ class Doctrine_Hydrate2 extends Doctrine_Access
{ {
return $this->_aliasMap; return $this->_aliasMap;
} }
/**
* mapAggregateValues
* map the aggregate values of given dataset row to a given record
*
* @param Doctrine_Record $record
* @param array $row
*/
public function mapAggregateValues($record, array $row)
{
// aggregate values have numeric keys
if (isset($row[0])) {
// map each aggregate value
foreach ($row as $index => $value) {
$agg = false;
if (isset($this->pendingAggregates[$alias][$index])) {
$agg = $this->pendingAggregates[$alias][$index][3];
} elseif (isset($this->subqueryAggregates[$alias][$index])) {
$agg = $this->subqueryAggregates[$alias][$index];
}
$record->mapValue($agg, $value);
}
}
return $record;
}
/** /**
* execute * execute
* executes the dql query and populates all collections * executes the dql query and populates all collections
...@@ -320,12 +348,17 @@ class Doctrine_Hydrate2 extends Doctrine_Access ...@@ -320,12 +348,17 @@ class Doctrine_Hydrate2 extends Doctrine_Access
*/ */
public function execute($params = array(), $return = Doctrine::FETCH_RECORD) public function execute($params = array(), $return = Doctrine::FETCH_RECORD)
{ {
$array = $this->_fetch($params = array(), $return = Doctrine::FETCH_RECORD); $array = (array) $this->_fetch($params = array(), $return = Doctrine::FETCH_RECORD);
$coll = new Doctrine_Collection(key($this->_aliasMap)); if (empty($this->_aliasMap)) {
$prev[$root] = $coll; throw new Doctrine_Hydrate_Exception("Couldn't execute query. Component alias map was empty.");
}
$previd = array(); $rootMap = current($this->_aliasMap);
$coll = new Doctrine_Collection($rootMap['table']);
$prev[key($this->_aliasMap)] = $coll;
$prevRow = array();
/** /**
* iterate over the fetched data * iterate over the fetched data
* here $data is a two dimensional array * here $data is a two dimensional array
...@@ -334,13 +367,47 @@ class Doctrine_Hydrate2 extends Doctrine_Access ...@@ -334,13 +367,47 @@ class Doctrine_Hydrate2 extends Doctrine_Access
/** /**
* remove duplicated data rows and map data into objects * remove duplicated data rows and map data into objects
*/ */
foreach ($data as $key => $row) { foreach ($data as $tableAlias => $row) {
if (empty($row)) { if (empty($row)) {
continue; continue;
} }
if ( ! isset($this->tableAliases[$tableAlias])) {
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
} }
$alias = $this->tableAliases[$tableAlias];
$map = $this->_aliasMap[$alias];
if ($prevRow[$tableAlias] !== $row) {
// set internal data
$this->tables[$name]->setData($row);
// initialize a new record
$record = $this->tables[$name]->getRecord();
$this->mapAggregateValues($record, $row);
if ($name == $root) {
// add record into root collection
$coll->add($record);
unset($previd);
} else {
$prev = $this->addRelated($prev, $name, $record);
}
// following statement is needed to ensure that mappings
// are being done properly when the result set doesn't
// contain the rows in 'right order'
if ($prev[$name] !== $record) {
$prev[$name] = $record;
}
}
$prevRow[$tableAlias] = $row;
}
} }
} }
/** /**
......
...@@ -40,10 +40,7 @@ require_once('../draft/new-core/Collection.php'); ...@@ -40,10 +40,7 @@ require_once('../draft/new-core/Collection.php');
class Doctrine_NewCore_TestCase extends Doctrine_UnitTestCase class Doctrine_NewCore_TestCase extends Doctrine_UnitTestCase
{ {
public function testHydrate() protected $testData1 = array(
{
$h = new Doctrine_Hydrate_Mock();
$h->setData(array(
array( array(
'e' => array('id' => 1, 'name' => 'zYne'), 'e' => array('id' => 1, 'name' => 'zYne'),
'p' => array('id' => 1, 'phonenumber' => '123 123', 'user_id' => 1) 'p' => array('id' => 1, 'phonenumber' => '123 123', 'user_id' => 1)
...@@ -56,9 +53,37 @@ class Doctrine_NewCore_TestCase extends Doctrine_UnitTestCase ...@@ -56,9 +53,37 @@ class Doctrine_NewCore_TestCase extends Doctrine_UnitTestCase
'e' => array('id' => 3, 'name' => 'Arnold'), 'e' => array('id' => 3, 'name' => 'Arnold'),
'p' => array('id' => 3, 'phonenumber' => '333 333', 'user_id' => 3) 'p' => array('id' => 3, 'phonenumber' => '333 333', 'user_id' => 3)
) )
)
); );
public function testExecuteForEmptyAliasMapThrowsException()
{
$h = new Doctrine_Hydrate_Mock();
try {
$h->execute();
$this->fail('Should throw exception');
} catch(Doctrine_Hydrate_Exception $e) {
$this->pass();
}
}
public function testExecuteForEmptyTableAliasMapThrowsException()
{
$h = new Doctrine_Hydrate_Mock();
$h->setData($this->testData1);
$h->setAliasMap(array('u' => array('table' => $this->conn->getTable('User'))));
try {
$h->execute();
$this->fail('Should throw exception');
} catch(Doctrine_Hydrate_Exception $e) {
$this->pass();
}
}
public function testHydrate()
{
$h = new Doctrine_Hydrate_Mock();
$h->setData($this->testData1);
$h->setAliasMap(array('u' => array('table' => $this->conn->getTable('User')))); $h->setAliasMap(array('u' => array('table' => $this->conn->getTable('User'))));
$h->setTableAliases(array('e' => 'u'));
} }
} }
class Doctrine_Hydrate_Mock extends Doctrine_Hydrate2 class Doctrine_Hydrate_Mock extends Doctrine_Hydrate2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment