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])) {
......
This diff is collapsed.
...@@ -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