From 3578ca55853d6dad27b64d7452116df0e91a852c Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 15 May 2007 14:36:09 +0000 Subject: [PATCH] --- draft/new-core/Collection.php | 3 +- draft/new-core/Hydrate.php | 46 ++++++++++++++++++----------- draft/new-core/Query.php | 55 +++++++++++++++-------------------- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/draft/new-core/Collection.php b/draft/new-core/Collection.php index 636eaeea9..4de0947ee 100644 --- a/draft/new-core/Collection.php +++ b/draft/new-core/Collection.php @@ -18,7 +18,7 @@ * and is licensed under the LGPL. For more information, see * . */ -Doctrine::autoload("Doctrine_Access"); +Doctrine::autoload('Doctrine_Access'); /** * Doctrine_Collection * Collection of Doctrine_Record objects. @@ -62,7 +62,6 @@ class Doctrine_Collection2 extends Doctrine_Collection implements Countable, Ite */ protected static $null; - protected $aggregateValues = array(); /** * constructor diff --git a/draft/new-core/Hydrate.php b/draft/new-core/Hydrate.php index 2170bb00c..44432adbc 100644 --- a/draft/new-core/Hydrate.php +++ b/draft/new-core/Hydrate.php @@ -155,6 +155,20 @@ class Doctrine_Hydrate2 { return $this->aliasHandler->getShortAlias($componentAlias); } + public function addQueryPart($name, $part) + { + if ( ! isset($this->parts[$name])) { + throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name); + } + $this->parts[$name][] = $part; + } + public function setQueryPart($name, $part) + { + if ( ! isset($this->parts[$name])) { + throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name); + } + $this->parts[$name] = array($part); + } /** * copyAliases * @@ -331,8 +345,9 @@ class Doctrine_Hydrate2 * @param array $row * @return Doctrine_Record */ - public function mapAggregateValues($record, array $row) + public function mapAggregateValues($record, array $row, $alias) { + $found = false; // aggregate values have numeric keys if (isset($row[0])) { // map each aggregate value @@ -345,9 +360,10 @@ class Doctrine_Hydrate2 $agg = $this->subqueryAggregates[$alias][$index]; } $record->mapValue($agg, $value); + $found = true; } - } - return $record; + } + return $found; } /** * execute @@ -384,11 +400,7 @@ class Doctrine_Hydrate2 if (empty($row)) { continue; } - // check for validity - if ( ! isset($this->tableAliases[$tableAlias])) { - throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias); - } - $alias = $this->tableAliases[$tableAlias]; + $alias = $this->aliasHandler->getComponentAlias($tableAlias); $map = $this->_aliasMap[$alias]; // initialize previous row array if not set @@ -398,20 +410,20 @@ class Doctrine_Hydrate2 // don't map duplicate rows if ($prevRow[$tableAlias] !== $row) { - // set internal data - $map['table']->setData($row); - $identifiable = $this->isIdentifiable($row, $map['table']->getIdentifier()); - - // only initialize record if the current data row is identifiable + if ($identifiable) { - // initialize a new record - $record = $map['table']->getRecord(); + // set internal data + $map['table']->setData($row); } + // initialize a new record + $record = $map['table']->getRecord(); // map aggregate values (if any) - $this->mapAggregateValues($record, $row); + if($this->mapAggregateValues($record, $row, $alias)) { + $identifiable = true; + } if ($alias == $rootAlias) { @@ -438,7 +450,7 @@ class Doctrine_Hydrate2 // one-to-many relation or many-to-many relation if ( ! $prev[$parentAlias]->getLast()->hasReference($relation->getAlias())) { // initialize a new collection - $prev[$alias] = new Doctrine_Collection2($parentMap['table']); + $prev[$alias] = new Doctrine_Collection2($map['table']); $prev[$alias]->setReference($parent, $relation); } else { // previous entry found from memory diff --git a/draft/new-core/Query.php b/draft/new-core/Query.php index bd9994c2e..155deb7fe 100644 --- a/draft/new-core/Query.php +++ b/draft/new-core/Query.php @@ -338,10 +338,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable } public function processPendingAggregates($componentAlias) { - $tableAlias = $this->getTableAlias($componentAlias); + $tableAlias = $this->getTableAlias($componentAlias); - reset($this->_aliasMap); - $map = current($this->tables); + $map = reset($this->_aliasMap); $root = $map['table']; $table = $this->_aliasMap[$componentAlias]['table']; @@ -373,7 +372,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable $e[1] = $table->getColumnName($e[1]); - if( ! $table->hasColumn($e[1])) { + if ( ! $table->hasColumn($e[1])) { throw new Doctrine_Query_Exception('Unknown column ' . $e[1]); } @@ -385,7 +384,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable $sqlAlias = $tableAlias . '__' . count($this->aggregateMap); - if(substr($name, 0, 1) !== '(') { + if (substr($name, 0, 1) !== '(') { $this->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $sqlAlias; } else { $this->parts['select'][] = $name . ' AS ' . $sqlAlias; @@ -542,10 +541,10 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable } } - $q .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']):''; - $q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']):''; - $q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']):''; - $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']):''; + $q .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : ''; + $q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : ''; + $q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']): ''; + $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : ''; if ($modifyLimit) { $q = $this->conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']); @@ -757,7 +756,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable case 'set': $class = 'Doctrine_Query_' . ucwords(strtolower($k)); $parser = new $class($this); - $this->parts['set'][] = $parser->parse($part); + $parser->parse($part); break; case 'group': case 'order': @@ -768,7 +767,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable $parser = new $class($this); $name = strtolower($k); - $this->parts[$name][] = $parser->parse($part); + $parser->parse($part); break; case 'limit': $this->parts['limit'] = trim($part); @@ -898,7 +897,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable if ($loadFields && empty($this->pendingFields) && empty($this->pendingAggregates) && empty($this->pendingSubqueries)) { - + $this->pendingFields[$componentAlias] = array('*'); $restoreState = true; @@ -918,6 +917,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable } } } + return end($this->_aliasMap); } /** * loadRoot @@ -959,7 +959,6 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { $oldParts = $this->parts; - $this->remove('select'); $join = $this->join; $where = $this->where; $having = $this->having; @@ -973,18 +972,11 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable $q .= ' '.implode(' ',$j); } $string = $this->applyInheritance(); - - if( ! empty($where)) { - $q .= ' WHERE ' . implode(' AND ', $where); - if( ! empty($string)) - $q .= ' AND (' . $string . ')'; - } else { - if( ! empty($string)) - $q .= ' WHERE (' . $string . ')'; + if ( ! empty($string)) { + $where[] = $string; } - - if( ! empty($having)) - $q .= ' HAVING ' . implode(' AND ',$having); + $q .= ( ! empty($where)) ? ' WHERE ' . implode(' AND ', $where) : ''; + $q .= ( ! empty($having)) ? ' HAVING ' . implode(' AND ', $having): ''; if( ! is_array($params)) { $params = array($params); @@ -992,7 +984,6 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable $params = array_merge($this->params, $params); - $this->parts = $oldParts; return (int) $this->getConnection()->fetchOne($q, $params); } /** @@ -1136,7 +1127,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable */ public function leftJoin($join) { - return $this->getParser('from')->parse('LERT JOIN ' . $join); + return $this->getParser('from')->parse('LEFT JOIN ' . $join); } /** * groupBy @@ -1187,12 +1178,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable * orderBy * sets the ORDER BY part of the query * - * @param string $groupby DQL ORDER BY part + * @param string $orderby DQL ORDER BY part * @return Doctrine_Query */ - public function orderBy($dql) + public function orderBy($orderby) { - return $this->getParser('orderby')->parse($dql); + return $this->getParser('orderby')->parse($orderby); } /** * limit @@ -1203,7 +1194,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable */ public function limit($limit) { - return $this->getParser('limit')->parse($dql); + return $this->getParser('limit')->parse($limit); } /** * offset @@ -1212,9 +1203,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable * @param integer $offset offset to be used for paginating the query * @return Doctrine_Query */ - public function offset($dql) + public function offset($offset) { - return $this->getParser('offset')->parse($dql); + return $this->getParser('offset')->parse($offset); } }