Commit 0dd85678 authored by romanb's avatar romanb

Continued work on new hydration.

parent f92773fa
...@@ -197,7 +197,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -197,7 +197,7 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
$oneToOne = false; $oneToOne = false;
// append element // append element
if (isset($nonemptyComponents[$dqlAlias])) { if (isset($nonemptyComponents[$dqlAlias])) {
$driver->initRelated($prev[$parent], $relationAlias); $driver->initRelatedCollection($prev[$parent], $relationAlias);
if ( ! isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]])) { if ( ! isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]])) {
$element = $driver->getElement($data, $componentName); $element = $driver->getElement($data, $componentName);
...@@ -225,6 +225,8 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -225,6 +225,8 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
} }
// register collection for later snapshots // register collection for later snapshots
//$driver->registerCollection($prev[$parent][$relationAlias]); //$driver->registerCollection($prev[$parent][$relationAlias]);
} else if ( ! isset($prev[$parent][$relationAlias])) {
$prev[$parent][$relationAlias] = $driver->getNullPointer();
} }
} else { } else {
// 1-1 relation // 1-1 relation
...@@ -236,10 +238,12 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract ...@@ -236,10 +238,12 @@ class Doctrine_Hydrator extends Doctrine_Hydrator_Abstract
$prev[$parent][$relationAlias] = $element; $prev[$parent][$relationAlias] = $element;
} }
} }
if ($prev[$parent][$relationAlias] !== null) {
$coll =& $prev[$parent][$relationAlias]; $coll =& $prev[$parent][$relationAlias];
$this->_setLastElement($prev, $coll, $index, $dqlAlias, $oneToOne); $this->_setLastElement($prev, $coll, $index, $dqlAlias, $oneToOne);
} }
} }
}
$stmt->closeCursor(); $stmt->closeCursor();
......
...@@ -66,7 +66,7 @@ class Doctrine_Hydrator_ArrayDriver ...@@ -66,7 +66,7 @@ class Doctrine_Hydrator_ArrayDriver
/** /**
* *
*/ */
public function initRelated(array &$data, $name) public function initRelatedCollection(array &$data, $name)
{ {
if ( ! isset($data[$name])) { if ( ! isset($data[$name])) {
$data[$name] = array(); $data[$name] = array();
......
...@@ -68,7 +68,7 @@ class Doctrine_Hydrator_RecordDriver ...@@ -68,7 +68,7 @@ class Doctrine_Hydrator_RecordDriver
} }
} }
public function initRelated(Doctrine_Record $record, $name) public function initRelatedCollection(Doctrine_Record $record, $name)
{ {
if ( ! isset($this->_initializedRelations[$record->getOid()][$name])) { if ( ! isset($this->_initializedRelations[$record->getOid()][$name])) {
$relation = $record->getClassMetadata()->getRelation($name); $relation = $record->getClassMetadata()->getRelation($name);
......
...@@ -20,8 +20,27 @@ ...@@ -20,8 +20,27 @@
*/ */
/** /**
* The hydrator has the tedious task to construct object or array graphs out of * The hydrator has the tedious to process result sets returned by the database
* a database result set. * and turn them into useable structures.
*
* Runtime complexity: The following gives the overall number of iterations
* required to process a result set.
*
* <code>numRowsInResult * numColumnsInResult + numRowsInResult * numClassesInQuery</code>
*
* This comes down to:
*
* <code>(numRowsInResult * (numColumnsInResult + numClassesInQuery))</code>
*
* Note that this is only a crude definition of the complexity as it also heavily
* depends on the complexity of all the single operations that are performed in
* each iteration.
*
* As can be seen, the number of columns in the result has the most impact on
* the overall performance (apart from the row counr, of course), since numClassesInQuery
* is usually pretty low.
* That's why the performance of the gatherRowData() method which is responsible
* for the "numRowsInResult * numColumnsInResult" part is crucial to fast hydraton.
* *
* @package Doctrine * @package Doctrine
* @subpackage Hydrator * @subpackage Hydrator
...@@ -153,9 +172,9 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -153,9 +172,9 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
// do we need to index by a custom field? // do we need to index by a custom field?
if ($field = $this->_getCustomIndexField($rootAlias)) { if ($field = $this->_getCustomIndexField($rootAlias)) {
if (isset($result[$field])) { if (isset($result[$field])) {
throw new Doctrine_Hydrator_Exception("Couldn't hydrate. Found non-unique key mapping."); throw Doctrine_Hydrator_Exception::nonUniqueKeyMapping();
} else if ( ! isset($element[$field])) { } else if ( ! isset($element[$field])) {
throw new Doctrine_Hydrator_Exception("Couldn't hydrate. Found a non-existent key."); throw Doctrine_Hydrator_Exception::nonExistantFieldUsedAsIndex($field);
} }
if ($this->_isResultMixed) { if ($this->_isResultMixed) {
$result[] = array($element[$field] => $element); $result[] = array($element[$field] => $element);
...@@ -169,7 +188,6 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -169,7 +188,6 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
$result[] = $element; $result[] = $element;
} }
} }
$identifierMap[$rootAlias][$id[$rootAlias]] = $driver->getLastKey($result); $identifierMap[$rootAlias][$id[$rootAlias]] = $driver->getLastKey($result);
} else { } else {
$index = $identifierMap[$rootAlias][$id[$rootAlias]]; $index = $identifierMap[$rootAlias][$id[$rootAlias]];
...@@ -185,7 +203,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -185,7 +203,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
unset($rowData['scalars']); unset($rowData['scalars']);
} }
// $prev[$rootAlias] now points to the last element in $result. // $resultPointers[$rootAlias] now points to the last element in $result.
// now hydrate the rest of the data found in the current row, that belongs to other // now hydrate the rest of the data found in the current row, that belongs to other
// (related) components. // (related) components.
foreach ($rowData as $dqlAlias => $data) { foreach ($rowData as $dqlAlias => $data) {
...@@ -204,8 +222,10 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -204,8 +222,10 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
$path = $parent . '.' . $dqlAlias; $path = $parent . '.' . $dqlAlias;
// pick the right element that will get the associated element attached
if ($this->_isResultMixed && $parent == $rootAlias) {
$key = key(reset($resultPointers)); $key = key(reset($resultPointers));
if ($this->_isResultMixed && $parent == $rootAlias && isset($resultPointers[$parent][$key])) { // TODO: Exception if $key === null ?
$baseElement =& $resultPointers[$parent][$key]; $baseElement =& $resultPointers[$parent][$key];
} else if (isset($resultPointers[$parent])) { } else if (isset($resultPointers[$parent])) {
$baseElement =& $resultPointers[$parent]; $baseElement =& $resultPointers[$parent];
...@@ -218,7 +238,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -218,7 +238,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
// x-many relation // x-many relation
$oneToOne = false; $oneToOne = false;
if (isset($nonemptyComponents[$dqlAlias])) { if (isset($nonemptyComponents[$dqlAlias])) {
$driver->initRelated($baseElement, $relationAlias); $driver->initRelatedCollection($baseElement, $relationAlias);
if ( ! isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]])) { if ( ! isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]])) {
$element = $driver->getElement($data, $componentName); $element = $driver->getElement($data, $componentName);
...@@ -244,6 +264,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -244,6 +264,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
} else { } else {
$index = $identifierMap[$path][$id[$parent]][$id[$dqlAlias]]; $index = $identifierMap[$path][$id[$parent]][$id[$dqlAlias]];
} }
} else if ( ! isset($baseElement[$relationAlias])) {
$baseElement[$relationAlias] = $driver->getNullPointer();
} }
} else { } else {
// x-1 relation // x-1 relation
...@@ -254,9 +276,11 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract ...@@ -254,9 +276,11 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
$baseElement[$relationAlias] = $driver->getElement($data, $componentName); $baseElement[$relationAlias] = $driver->getElement($data, $componentName);
} }
} }
if ($baseElement[$relationAlias] !== null) {
$coll =& $baseElement[$relationAlias]; $coll =& $baseElement[$relationAlias];
$this->_setLastElement($resultPointers, $coll, $index, $dqlAlias, $oneToOne); $this->_setLastElement($resultPointers, $coll, $index, $dqlAlias, $oneToOne);
} }
}
// append scalar values // append scalar values
if (isset($scalars)) { if (isset($scalars)) {
......
...@@ -615,7 +615,9 @@ class Doctrine_Mapper ...@@ -615,7 +615,9 @@ class Doctrine_Mapper
public function saveAssociations(Doctrine_Record $record) public function saveAssociations(Doctrine_Record $record)
{ {
foreach ($record->getReferences() as $relationName => $relatedObject) { foreach ($record->getReferences() as $relationName => $relatedObject) {
if ($relatedObject === Doctrine_Null::$INSTANCE) {
continue;
}
$rel = $record->getTable()->getRelation($relationName); $rel = $record->getTable()->getRelation($relationName);
if ($rel instanceof Doctrine_Relation_Association) { if ($rel instanceof Doctrine_Relation_Association) {
......
...@@ -928,6 +928,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -928,6 +928,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$rel = $this->_class->getRelation($fieldName); $rel = $this->_class->getRelation($fieldName);
$this->_references[$fieldName] = $rel->fetchRelatedFor($this); $this->_references[$fieldName] = $rel->fetchRelatedFor($this);
} }
if ($this->_references[$fieldName] === Doctrine_Null::$INSTANCE) {
return null;
}
return $this->_references[$fieldName]; return $this->_references[$fieldName];
} catch (Doctrine_Relation_Exception $e) { } catch (Doctrine_Relation_Exception $e) {
//echo $e->getTraceAsString(); //echo $e->getTraceAsString();
...@@ -1038,6 +1041,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1038,6 +1041,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
*/ */
private function _coreSetRelated($name, $value) private function _coreSetRelated($name, $value)
{ {
if ($value === Doctrine_Null::$INSTANCE) {
$this->_references[$name] = $value;
return;
}
$rel = $this->_class->getRelation($name); $rel = $this->_class->getRelation($name);
// one-to-many or one-to-one relation // one-to-many or one-to-one relation
...@@ -1045,7 +1053,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1045,7 +1053,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$rel instanceof Doctrine_Relation_LocalKey) { $rel instanceof Doctrine_Relation_LocalKey) {
if ( ! $rel->isOneToOne()) { if ( ! $rel->isOneToOne()) {
// one-to-many relation found // one-to-many relation found
if ( ! ($value instanceof Doctrine_Collection)) { if ( ! $value instanceof Doctrine_Collection) {
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second" throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second"
. " argument should be an instance of Doctrine_Collection when" . " argument should be an instance of Doctrine_Collection when"
. " setting one-to-many references."); . " setting one-to-many references.");
...@@ -1067,7 +1075,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1067,7 +1075,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
. " or Doctrine_Null when setting one-to-one references."); . " or Doctrine_Null when setting one-to-one references.");
} }
if ($rel instanceof Doctrine_Relation_LocalKey) { if ($rel instanceof Doctrine_Relation_LocalKey) {
$idFieldNames = (array)$value->getTable()->getIdentifier(); $idFieldNames = $value->getTable()->getIdentifier();
if ( ! empty($foreignFieldName) && $foreignFieldName != $idFieldNames[0]) { if ( ! empty($foreignFieldName) && $foreignFieldName != $idFieldNames[0]) {
$this->set($localFieldName, $value->rawGet($foreignFieldName), false); $this->set($localFieldName, $value->rawGet($foreignFieldName), false);
} else { } else {
...@@ -1097,6 +1105,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1097,6 +1105,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public function contains($fieldName) public function contains($fieldName)
{ {
if (isset($this->_data[$fieldName])) { if (isset($this->_data[$fieldName])) {
if ($this->_data[$fieldName] === Doctrine_Null::$INSTANCE) {
return false;
}
return true; return true;
} }
if (isset($this->_id[$fieldName])) { if (isset($this->_id[$fieldName])) {
......
...@@ -9,22 +9,21 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase ...@@ -9,22 +9,21 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
parent::setUp(); parent::setUp();
} }
/** The data of the hydration mode dataProvider */
protected static $hydrationModeProviderData = array(
array('hydrationMode' => Doctrine::HYDRATE_RECORD),
array('hydrationMode' => Doctrine::HYDRATE_ARRAY)
);
/** Getter for the hydration mode dataProvider */ /** Getter for the hydration mode dataProvider */
public static function hydrationModeProvider() public static function hydrationModeProvider()
{ {
return self::$hydrationModeProviderData; return array(
array('hydrationMode' => Doctrine::HYDRATE_RECORD),
array('hydrationMode' => Doctrine::HYDRATE_ARRAY)
);
} }
/** /**
* Select u.id, u.name from CmsUser u * Select u.id, u.name from CmsUser u
* *
* @dataProvider hydrationModeProvider
*/ */
public function testBasicHydration() public function testNewHydrationSimpleEntityQuery($hydrationMode)
{ {
// Faked query components // Faked query components
$queryComponents = array( $queryComponents = array(
...@@ -44,12 +43,10 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase ...@@ -44,12 +43,10 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
// Faked result set // Faked result set
$resultSet = array( $resultSet = array(
//row1
array( array(
'u__id' => '1', 'u__id' => '1',
'u__name' => 'romanb' 'u__name' => 'romanb'
), ),
//row2
array( array(
'u__id' => '2', 'u__id' => '2',
'u__name' => 'jwage' 'u__name' => 'jwage'
...@@ -58,30 +55,24 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase ...@@ -58,30 +55,24 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
$stmt = new Doctrine_HydratorMockStatement($resultSet); $stmt = new Doctrine_HydratorMockStatement($resultSet);
$hydrator = new Doctrine_Hydrator(); $hydrator = new Doctrine_HydratorNew();
$hydrator->setQueryComponents($queryComponents); $hydrator->setQueryComponents($queryComponents);
$arrayResult = $hydrator->hydrateResultSet($stmt, $tableAliasMap, Doctrine::HYDRATE_ARRAY); $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode);
$this->assertTrue(is_array($arrayResult));
$this->assertEquals(2, count($arrayResult));
$this->assertEquals(1, $arrayResult[0]['id']);
$this->assertEquals('romanb', $arrayResult[0]['name']);
$this->assertEquals(2, $arrayResult[1]['id']);
$this->assertEquals('jwage', $arrayResult[1]['name']);
$stmt->setResultSet($resultSet);
$objectResult = $hydrator->hydrateResultSet($stmt, $tableAliasMap, Doctrine::HYDRATE_RECORD);
$this->assertTrue($objectResult instanceof Doctrine_Collection); $this->assertEquals(2, count($result));
$this->assertEquals(2, count($objectResult)); $this->assertEquals(1, $result[0]['id']);
$this->assertTrue($objectResult[0] instanceof Doctrine_Record); $this->assertEquals('romanb', $result[0]['name']);
$this->assertEquals(1, $objectResult[0]->id); $this->assertEquals(2, $result[1]['id']);
$this->assertEquals('romanb', $objectResult[0]->name); $this->assertEquals('jwage', $result[1]['name']);
$this->assertTrue($objectResult[1] instanceof Doctrine_Record);
$this->assertEquals(2, $objectResult[1]->id);
$this->assertEquals('jwage', $objectResult[1]->name);
if ($hydrationMode == Doctrine::HYDRATE_RECORD) {
$this->assertTrue($result instanceof Doctrine_Collection);
$this->assertTrue($result[0] instanceof Doctrine_Record);
$this->assertTrue($result[1] instanceof Doctrine_Record);
} else {
$this->assertTrue(is_array($result));
}
} }
/** /**
...@@ -188,7 +179,7 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase ...@@ -188,7 +179,7 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
* *
* @dataProvider hydrationModeProvider * @dataProvider hydrationModeProvider
*/ */
public function testNewHydrationBasicsMixedQueryNormalJoin($hydrationMode) public function testNewHydrationMixedQueryNormalJoin($hydrationMode)
{ {
// Faked query components // Faked query components
$queryComponents = array( $queryComponents = array(
...@@ -354,11 +345,10 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase ...@@ -354,11 +345,10 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
$this->assertTrue($result[0]['1']['phonenumbers'] instanceof Doctrine_Collection); $this->assertTrue($result[0]['1']['phonenumbers'] instanceof Doctrine_Collection);
$this->assertEquals(2, count($result[0]['1']['phonenumbers'])); $this->assertEquals(2, count($result[0]['1']['phonenumbers']));
} }
} }
/** /**
* select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic
* from User u * from User u
* join u.phonenumbers p * join u.phonenumbers p
...@@ -509,8 +499,304 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase ...@@ -509,8 +499,304 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
} }
} }
/**
* select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic,
* c.id, c.topic
* from User u
* join u.phonenumbers p
* join u.articles a
* left join a.comments c
* =
* select u.id, u.status, p.phonenumber, upper(u.name) as u__0, a.id, a.topic,
* c.id, c.topic
* from USERS u
* inner join PHONENUMBERS p ON u.id = p.user_id
* inner join ARTICLES a ON u.id = a.user_id
* left outer join COMMENTS c ON a.id = c.article_id
*
* @dataProvider hydrationModeProvider
*/
public function testNewHydrationMixedQueryMultipleDeepMixedFetchJoin($hydrationMode)
{
// Faked query components
$queryComponents = array(
'u' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'),
'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'),
'parent' => null,
'relation' => null,
'map' => null,
'agg' => array('0' => 'nameUpper')
),
'p' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('CmsPhonenumber'),
'mapper' => $this->sharedFixture['connection']->getMapper('CmsPhonenumber'),
'parent' => 'u',
'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('phonenumbers'),
'map' => null
),
'a' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('CmsArticle'),
'mapper' => $this->sharedFixture['connection']->getMapper('CmsArticle'),
'parent' => 'u',
'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('articles'),
'map' => null
),
'c' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('CmsComment'),
'mapper' => $this->sharedFixture['connection']->getMapper('CmsComment'),
'parent' => 'a',
'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsArticle')->getRelation('comments'),
'map' => null
),
);
// Faked table alias map
$tableAliasMap = array(
'u' => 'u',
'p' => 'p',
'a' => 'a',
'c' => 'c'
);
// Faked result set
$resultSet = array(
//row1
array(
'u__id' => '1',
'u__status' => 'developer',
'u__0' => 'ROMANB',
'p__phonenumber' => '42',
'a__id' => '1',
'a__topic' => 'Getting things done!',
'c__id' => '1',
'c__topic' => 'First!'
),
array(
'u__id' => '1',
'u__status' => 'developer',
'u__0' => 'ROMANB',
'p__phonenumber' => '43',
'a__id' => '1',
'a__topic' => 'Getting things done!',
'c__id' => '1',
'c__topic' => 'First!'
),
array(
'u__id' => '1',
'u__status' => 'developer',
'u__0' => 'ROMANB',
'p__phonenumber' => '42',
'a__id' => '2',
'a__topic' => 'ZendCon',
'c__id' => null,
'c__topic' => null
),
array(
'u__id' => '1',
'u__status' => 'developer',
'u__0' => 'ROMANB',
'p__phonenumber' => '43',
'a__id' => '2',
'a__topic' => 'ZendCon',
'c__id' => null,
'c__topic' => null
),
array(
'u__id' => '2',
'u__status' => 'developer',
'u__0' => 'JWAGE',
'p__phonenumber' => '91',
'a__id' => '3',
'a__topic' => 'LINQ',
'c__id' => null,
'c__topic' => null
),
array(
'u__id' => '2',
'u__status' => 'developer',
'u__0' => 'JWAGE',
'p__phonenumber' => '91',
'a__id' => '4',
'a__topic' => 'PHP6',
'c__id' => null,
'c__topic' => null
),
);
$stmt = new Doctrine_HydratorMockStatement($resultSet);
$hydrator = new Doctrine_HydratorNew();
$hydrator->setQueryComponents($queryComponents);
$hydrator->setResultMixed(true);
$result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode);
if ($hydrationMode == Doctrine::HYDRATE_ARRAY) {
//var_dump($result);
}
$this->assertEquals(2, count($result));
$this->assertTrue(is_array($result));
$this->assertTrue(is_array($result[0]));
$this->assertTrue(is_array($result[1]));
// first user => 2 phonenumbers, 2 articles, 1 comment on first article
$this->assertEquals(2, count($result[0][0]['phonenumbers']));
$this->assertEquals(2, count($result[0][0]['articles']));
$this->assertEquals(1, count($result[0][0]['articles'][0]['comments']));
$this->assertEquals('ROMANB', $result[0]['nameUpper']);
// second user => 1 phonenumber, 2 articles, no comments
$this->assertEquals(1, count($result[1][0]['phonenumbers']));
$this->assertEquals(2, count($result[1][0]['articles']));
$this->assertEquals('JWAGE', $result[1]['nameUpper']);
$this->assertEquals(42, $result[0][0]['phonenumbers'][0]['phonenumber']);
$this->assertEquals(43, $result[0][0]['phonenumbers'][1]['phonenumber']);
$this->assertEquals(91, $result[1][0]['phonenumbers'][0]['phonenumber']);
$this->assertEquals('Getting things done!', $result[0][0]['articles'][0]['topic']);
$this->assertEquals('ZendCon', $result[0][0]['articles'][1]['topic']);
$this->assertEquals('LINQ', $result[1][0]['articles'][0]['topic']);
$this->assertEquals('PHP6', $result[1][0]['articles'][1]['topic']);
$this->assertEquals('First!', $result[0][0]['articles'][0]['comments'][0]['topic']);
$this->assertTrue(isset($result[0][0]['articles'][0]['comments']));
$this->assertFalse(isset($result[0][0]['articles'][1]['comments']));
$this->assertFalse(isset($result[1][0]['articles'][0]['comments']));
$this->assertFalse(isset($result[1][0]['articles'][1]['comments']));
if ($hydrationMode == Doctrine::HYDRATE_RECORD) {
$this->assertTrue($result[0][0] instanceof Doctrine_Record);
$this->assertTrue($result[1][0] instanceof Doctrine_Record);
// phonenumbers
$this->assertTrue($result[0][0]['phonenumbers'] instanceof Doctrine_Collection);
$this->assertTrue($result[0][0]['phonenumbers'][0] instanceof Doctrine_Record);
$this->assertTrue($result[0][0]['phonenumbers'][1] instanceof Doctrine_Record);
$this->assertTrue($result[1][0]['phonenumbers'] instanceof Doctrine_Collection);
$this->assertTrue($result[1][0]['phonenumbers'][0] instanceof Doctrine_Record);
// articles
$this->assertTrue($result[0][0]['articles'] instanceof Doctrine_Collection);
$this->assertTrue($result[0][0]['articles'][0] instanceof Doctrine_Record);
$this->assertTrue($result[0][0]['articles'][1] instanceof Doctrine_Record);
$this->assertTrue($result[1][0]['articles'][0] instanceof Doctrine_Record);
$this->assertTrue($result[1][0]['articles'][1] instanceof Doctrine_Record);
// article comments
$this->assertTrue($result[0][0]['articles'][0]['comments'] instanceof Doctrine_Collection);
$this->assertTrue($result[0][0]['articles'][0]['comments'][0] instanceof Doctrine_Record);
}
}
/**
* Tests that the hydrator does not rely on a particular order of the rows
* in the result set.
*
* DQL:
* select c.id, c.position, c.name, b.id, b.position
* from ForumCategory c inner join c.boards b
* order by c.position asc, b.position asc
*
* Checks whether the boards are correctly assigned to the categories.
*
* The 'evil' result set that confuses the object population is displayed below.
*
* c.id | c.position | c.name | boardPos | b.id | b.category_id (just for clarity)
* 1 | 0 | First | 0 | 1 | 1
* 2 | 0 | Second | 0 | 2 | 2 <--
* 1 | 0 | First | 1 | 3 | 1
* 1 | 0 | First | 2 | 4 | 1
*
* @dataProvider hydrationModeProvider
*/
public function testNewHydrationEntityQueryCustomResultSetOrder($hydrationMode)
{
// Faked query components
$queryComponents = array(
'c' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('ForumCategory'),
'mapper' => $this->sharedFixture['connection']->getMapper('ForumCategory'),
'parent' => null,
'relation' => null,
'map' => null
),
'b' => array(
'table' => $this->sharedFixture['connection']->getClassMetadata('ForumBoard'),
'mapper' => $this->sharedFixture['connection']->getMapper('ForumBoard'),
'parent' => 'c',
'relation' => $this->sharedFixture['connection']->getClassMetadata('ForumCategory')->getRelation('boards'),
'map' => null
),
);
// Faked table alias map
$tableAliasMap = array(
'c' => 'c',
'b' => 'b'
);
// Faked result set
$resultSet = array(
array(
'c__id' => '1',
'c__position' => '0',
'c__name' => 'First',
'b__id' => '1',
'b__position' => '0',
'b__category_id' => '1'
),
array(
'c__id' => '2',
'c__position' => '0',
'c__name' => 'Second',
'b__id' => '2',
'b__position' => '0',
'b__category_id' => '2'
),
array(
'c__id' => '1',
'c__position' => '0',
'c__name' => 'First',
'b__id' => '3',
'b__position' => '1',
'b__category_id' => '1'
),
array(
'c__id' => '1',
'c__position' => '0',
'c__name' => 'First',
'b__id' => '4',
'b__position' => '2',
'b__category_id' => '1'
)
);
$stmt = new Doctrine_HydratorMockStatement($resultSet);
$hydrator = new Doctrine_HydratorNew();
$hydrator->setQueryComponents($queryComponents);
//$hydrator->setResultMixed(true);
$result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode);
if ($hydrationMode == Doctrine::HYDRATE_ARRAY) {
//var_dump($result);
}
$this->assertEquals(2, count($result));
$this->assertTrue(isset($result[0]['boards']));
$this->assertEquals(3, count($result[0]['boards']));
$this->assertTrue(isset($result[1]['boards']));
$this->assertEquals(1, count($result[1]['boards']));
if ($hydrationMode == Doctrine::HYDRATE_ARRAY) {
$this->assertTrue(is_array($result));
$this->assertTrue(is_array($result[0]));
$this->assertTrue(is_array($result[1]));
} else {
$this->assertTrue($result instanceof Doctrine_Collection);
$this->assertTrue($result[0] instanceof Doctrine_Record);
$this->assertTrue($result[1] instanceof Doctrine_Record);
}
}
} }
\ No newline at end of file
...@@ -7,5 +7,7 @@ class CmsArticle extends Doctrine_Record ...@@ -7,5 +7,7 @@ class CmsArticle extends Doctrine_Record
$class->mapColumn('topic', 'string', 255); $class->mapColumn('topic', 'string', 255);
$class->mapColumn('text', 'string'); $class->mapColumn('text', 'string');
$class->mapColumn('user_id', 'integer', 4); $class->mapColumn('user_id', 'integer', 4);
$class->hasMany('CmsComment as comments', array(
'local' => 'id', 'foreign' => 'article_id'));
} }
} }
<?php
class CmsComment extends Doctrine_Record
{
public static function initMetadata($class)
{
$class->mapColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true));
$class->mapColumn('topic', 'string', 255);
$class->mapColumn('text', 'string');
$class->mapColumn('article_id', 'integer', 4);
}
}
<?php
class ForumBoard extends Doctrine_Record {
public static function initMetadata($class) {
$class->mapColumn('position', 'integer');
$class->mapColumn('category_id', 'integer');
$class->hasOne('ForumCategory as category',
array('local' => 'category_id', 'foreign' => 'id'));
}
}
<?php
class ForumCategory extends Doctrine_Record {
public static function initMetadata($class) {
$class->mapColumn('position', 'integer');
$class->mapColumn('name', 'string', 255);
$class->hasMany('ForumBoard as boards', array(
'local' => 'id' , 'foreign' => 'category_id'));
}
}
...@@ -49,10 +49,10 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase ...@@ -49,10 +49,10 @@ class Doctrine_Access_TestCase extends Doctrine_UnitTestCase
{ {
$user = new User(); $user = new User();
$this->assertTrue(isset($user->name)); $this->assertTrue(!isset($user->name));
$this->assertFalse(isset($user->unknown)); $this->assertFalse(isset($user->unknown));
$this->assertTrue(isset($user['name'])); $this->assertTrue(!isset($user['name']));
$this->assertFalse(isset($user['unknown'])); $this->assertFalse(isset($user['unknown']));
$coll = new Doctrine_Collection('User'); $coll = new Doctrine_Collection('User');
......
...@@ -169,7 +169,7 @@ class Doctrine_CustomResultSetOrder_TestCase extends Doctrine_UnitTestCase { ...@@ -169,7 +169,7 @@ class Doctrine_CustomResultSetOrder_TestCase extends Doctrine_UnitTestCase {
case 'Third': case 'Third':
// The third has no boards as expected. // The third has no boards as expected.
//print $category->Boards[0]->position; //print $category->Boards[0]->position;
$this->assertEqual(0, $category->Boards->count()); $this->assertTrue(!isset($category->Boards));
break; break;
} }
......
...@@ -115,7 +115,7 @@ class Doctrine_Record_Synchronize_TestCase extends Doctrine_UnitTestCase ...@@ -115,7 +115,7 @@ class Doctrine_Record_Synchronize_TestCase extends Doctrine_UnitTestCase
public function testSynchronizeAfterRemoveRecord() public function testSynchronizeAfterRemoveRecord()
{ {
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne(); $user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
$this->assertEqual($user->Phonenumber->count(), 0); $this->assertTrue(!isset($user->Phonenumber));
$this->assertTrue(!isset($user->Email)); $this->assertTrue(!isset($user->Email));
} }
} }
...@@ -113,9 +113,9 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase ...@@ -113,9 +113,9 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase
$user = new User(); $user = new User();
$this->assertTrue(isset($user->id)); $this->assertTrue(!isset($user->id));
$this->assertTrue(isset($user['id'])); $this->assertTrue(!isset($user['id']));
$this->assertTrue($user->contains('id')); $this->assertTrue(!$user->contains('id'));
} }
public function testNotNullConstraint() public function testNotNullConstraint()
......
...@@ -83,7 +83,7 @@ class Doctrine_Relation_OneToOne_TestCase extends Doctrine_UnitTestCase ...@@ -83,7 +83,7 @@ class Doctrine_Relation_OneToOne_TestCase extends Doctrine_UnitTestCase
$this->assertTrue($user->Email instanceOf Email); $this->assertTrue($user->Email instanceOf Email);
$user->Email = Doctrine_Null::$INSTANCE; $user->Email = Doctrine_Null::$INSTANCE;
$user->save(); $user->save();
$this->assertTrue($user->Email instanceOf Doctrine_Null); $this->assertTrue($user->Email === null);
} }
public function testSavingRelatedObjects() public function testSavingRelatedObjects()
......
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