Commit ca213ac6 authored by zYne's avatar zYne

Refactored Doctrine_Hydrate

parent cded682e
......@@ -277,6 +277,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
case 0:
throw new Doctrine_Exception("No tables selected");
break;
case 1:
$keys = array_keys($this->tables);
......@@ -301,7 +302,9 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
return $this->getCollection($keys[0]);
break;
default:
$keys = array_keys($this->tables);
$root = $keys[0];
......@@ -335,28 +338,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
if($this->isIdentifiable($row, $ids)) {
$pointer = $this->joins[$name];
$path = array_search($name, $this->tableAliases);
$tmp = explode(".", $path);
$alias = end($tmp);
unset($tmp);
$fk = $this->tables[$pointer]->getRelation($alias);
if( ! isset($prev[$pointer]) )
continue;
$last = $prev[$pointer]->getLast();
if( ! $fk->isOneToOne()) {
if($last instanceof Doctrine_Record) {
if( ! $last->hasReference($alias)) {
$prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name],$fk);
}
}
}
$prev = $this->initRelated($prev, $name);
continue;
}
......@@ -380,51 +362,89 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
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;
}
$previd[$name] = $row;
}
}
return $coll;
endswitch;
}
/**
* initRelation
*
* @param array $prev
* @param string $name
* @return array
*/
public function initRelated(array $prev, $name) {
$pointer = $this->joins[$name];
$path = array_search($name, $this->tableAliases);
$tmp = explode(".", $path);
$alias = end($tmp);
$fk = $this->tables[$pointer]->getRelation($alias);
if( ! isset($prev[$pointer]) )
return $prev;
if( ! $fk->isOneToOne()) {
if($prev[$pointer]->getLast() instanceof Doctrine_Record) {
if( ! $prev[$pointer]->getLast()->hasReference($alias)) {
$prev[$name] = $this->getCollection($name);
$prev[$pointer]->getLast()->initReference($prev[$name],$fk);
}
}
}
return $prev;
}
/**
* addRelated
*
* @param array $prev
* @param string $name
* @return array
*/
public function addRelated(array $prev, $name, Doctrine_Record $record) {
$pointer = $this->joins[$name];
$path = array_search($name, $this->tableAliases);
$tmp = explode(".", $path);
$alias = end($tmp);
unset($tmp);
$fk = $this->tables[$pointer]->getRelation($alias);
$last = $prev[$pointer]->getLast();
if($fk->isOneToOne()) {
$last->set($fk->getAlias(), $record);
$prev[$pointer]->getLast()->set($fk->getAlias(), $record);
$prev[$name] = $record;
} else {
// one-to-many relation or many-to-many relation
if( ! $last->hasReference($alias)) {
if( ! $prev[$pointer]->getLast()->hasReference($alias)) {
$prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name], $fk);
$prev[$pointer]->getLast()->initReference($prev[$name], $fk);
} else {
// previous entry found from memory
$prev[$name] = $last->get($alias);
}
$last->addReference($record, $fk);
}
}
// following statement is needed to ensure that mappings are being done properly when
// the result set doesn't contain the rows in 'right order' the
if($prev[$name] !== $record)
$prev[$name] = $record;
$prev[$name] = $prev[$pointer]->getLast()->get($alias);
}
$previd[$name] = $row;
$prev[$pointer]->getLast()->addReference($record, $fk);
}
return $prev;
}
return $coll;
endswitch;
}
/**
* isIdentifiable
* returns whether or not a given data row is identifiable (it contains
......
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