1
0
mirror of synced 2025-01-19 06:51:40 +03:00

Simplified object population mechanism

This commit is contained in:
zYne 2006-10-01 15:18:04 +00:00
parent ca213ac6b9
commit 5de6c0a65b

View File

@ -273,112 +273,82 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
if($this->aggregate) if($this->aggregate)
return $stmt->fetchAll(PDO::FETCH_ASSOC); return $stmt->fetchAll(PDO::FETCH_ASSOC);
switch(count($this->tables)): if(count($this->tables) == 0)
case 0: throw new Doctrine_Exception("No tables selected");
throw new Doctrine_Exception("No tables selected");
break;
case 1: $keys = array_keys($this->tables);
$keys = array_keys($this->tables); $root = $keys[0];
$name = $this->tables[$keys[0]]->getComponentName(); $previd = array();
$coll = $this->getCollection($root);
$prev[$root] = $coll;
while($data = $stmt->fetch(PDO::FETCH_ASSOC)): if($this->aggregate)
$return = Doctrine::FETCH_ARRAY;
foreach($data as $key => $value): $array = $this->parseData($stmt);
$e = explode("__",$key);
if(count($e) > 1) {
$data[end($e)] = $value;
} else {
$data[$e[0]] = $value;
}
unset($data[$key]);
endforeach;
$this->data[$name][] = $data;
endwhile;
return $this->getCollection($keys[0]); if($return == Doctrine::FETCH_ARRAY)
break; return $array;
default:
$keys = array_keys($this->tables);
$root = $keys[0];
$previd = array();
$coll = $this->getCollection($root);
$prev[$root] = $coll;
if($this->aggregate)
$return = Doctrine::FETCH_ARRAY;
$array = $this->parseData($stmt);
if($return == Doctrine::FETCH_ARRAY)
return $array;
foreach($array as $data) { foreach($array as $data) {
/** /**
* 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 $key => $row) {
if(empty($row)) if(empty($row))
continue; continue;
$ids = $this->tables[$key]->getIdentifier(); $ids = $this->tables[$key]->getIdentifier();
$name = $key; $name = $key;
if($this->isIdentifiable($row, $ids)) { if($this->isIdentifiable($row, $ids)) {
$prev = $this->initRelated($prev, $name); $prev = $this->initRelated($prev, $name);
continue; continue;
}
if( ! isset($previd[$name]))
$previd[$name] = array();
if($previd[$name] !== $row) {
// set internal data
$this->tables[$name]->setData($row);
// initialize a new record
$record = $this->tables[$name]->getRecord();
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;
}
$previd[$name] = $row;
}
} }
return $coll;
endswitch; if( ! isset($previd[$name]))
$previd[$name] = array();
if($previd[$name] !== $row) {
// set internal data
$this->tables[$name]->setData($row);
// initialize a new record
$record = $this->tables[$name]->getRecord();
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;
}
$previd[$name] = $row;
}
}
return $coll;
} }
/** /**
* initRelation * initRelation