1
0
mirror of synced 2024-12-14 07:06:04 +03:00
This commit is contained in:
zYne 2007-05-15 14:36:09 +00:00
parent dce54885e4
commit 3578ca5585
3 changed files with 53 additions and 51 deletions

View File

@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload("Doctrine_Access"); Doctrine::autoload('Doctrine_Access');
/** /**
* Doctrine_Collection * Doctrine_Collection
* Collection of Doctrine_Record objects. * Collection of Doctrine_Record objects.
@ -62,7 +62,6 @@ class Doctrine_Collection2 extends Doctrine_Collection implements Countable, Ite
*/ */
protected static $null; protected static $null;
protected $aggregateValues = array();
/** /**
* constructor * constructor

View File

@ -155,6 +155,20 @@ class Doctrine_Hydrate2
{ {
return $this->aliasHandler->getShortAlias($componentAlias); 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 * copyAliases
* *
@ -331,8 +345,9 @@ class Doctrine_Hydrate2
* @param array $row * @param array $row
* @return Doctrine_Record * @return Doctrine_Record
*/ */
public function mapAggregateValues($record, array $row) public function mapAggregateValues($record, array $row, $alias)
{ {
$found = false;
// aggregate values have numeric keys // aggregate values have numeric keys
if (isset($row[0])) { if (isset($row[0])) {
// map each aggregate value // map each aggregate value
@ -345,9 +360,10 @@ class Doctrine_Hydrate2
$agg = $this->subqueryAggregates[$alias][$index]; $agg = $this->subqueryAggregates[$alias][$index];
} }
$record->mapValue($agg, $value); $record->mapValue($agg, $value);
$found = true;
} }
} }
return $record; return $found;
} }
/** /**
* execute * execute
@ -384,11 +400,7 @@ class Doctrine_Hydrate2
if (empty($row)) { if (empty($row)) {
continue; continue;
} }
// check for validity $alias = $this->aliasHandler->getComponentAlias($tableAlias);
if ( ! isset($this->tableAliases[$tableAlias])) {
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
}
$alias = $this->tableAliases[$tableAlias];
$map = $this->_aliasMap[$alias]; $map = $this->_aliasMap[$alias];
// initialize previous row array if not set // initialize previous row array if not set
@ -398,20 +410,20 @@ class Doctrine_Hydrate2
// don't map duplicate rows // don't map duplicate rows
if ($prevRow[$tableAlias] !== $row) { if ($prevRow[$tableAlias] !== $row) {
// set internal data
$map['table']->setData($row);
$identifiable = $this->isIdentifiable($row, $map['table']->getIdentifier()); $identifiable = $this->isIdentifiable($row, $map['table']->getIdentifier());
// only initialize record if the current data row is identifiable
if ($identifiable) { if ($identifiable) {
// initialize a new record // set internal data
$record = $map['table']->getRecord(); $map['table']->setData($row);
} }
// initialize a new record
$record = $map['table']->getRecord();
// map aggregate values (if any) // map aggregate values (if any)
$this->mapAggregateValues($record, $row); if($this->mapAggregateValues($record, $row, $alias)) {
$identifiable = true;
}
if ($alias == $rootAlias) { if ($alias == $rootAlias) {
@ -438,7 +450,7 @@ class Doctrine_Hydrate2
// one-to-many relation or many-to-many relation // one-to-many relation or many-to-many relation
if ( ! $prev[$parentAlias]->getLast()->hasReference($relation->getAlias())) { if ( ! $prev[$parentAlias]->getLast()->hasReference($relation->getAlias())) {
// initialize a new collection // initialize a new collection
$prev[$alias] = new Doctrine_Collection2($parentMap['table']); $prev[$alias] = new Doctrine_Collection2($map['table']);
$prev[$alias]->setReference($parent, $relation); $prev[$alias]->setReference($parent, $relation);
} else { } else {
// previous entry found from memory // previous entry found from memory

View File

@ -340,8 +340,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
{ {
$tableAlias = $this->getTableAlias($componentAlias); $tableAlias = $this->getTableAlias($componentAlias);
reset($this->_aliasMap); $map = reset($this->_aliasMap);
$map = current($this->tables);
$root = $map['table']; $root = $map['table'];
$table = $this->_aliasMap[$componentAlias]['table']; $table = $this->_aliasMap[$componentAlias]['table'];
@ -373,7 +372,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
$e[1] = $table->getColumnName($e[1]); $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]); 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); $sqlAlias = $tableAlias . '__' . count($this->aggregateMap);
if(substr($name, 0, 1) !== '(') { if (substr($name, 0, 1) !== '(') {
$this->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $sqlAlias; $this->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $sqlAlias;
} else { } else {
$this->parts['select'][] = $name . ' AS ' . $sqlAlias; $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['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : '';
$q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']):''; $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['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']): '';
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']):''; $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
if ($modifyLimit) { if ($modifyLimit) {
$q = $this->conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']); $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': case 'set':
$class = 'Doctrine_Query_' . ucwords(strtolower($k)); $class = 'Doctrine_Query_' . ucwords(strtolower($k));
$parser = new $class($this); $parser = new $class($this);
$this->parts['set'][] = $parser->parse($part); $parser->parse($part);
break; break;
case 'group': case 'group':
case 'order': case 'order':
@ -768,7 +767,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
$parser = new $class($this); $parser = new $class($this);
$name = strtolower($k); $name = strtolower($k);
$this->parts[$name][] = $parser->parse($part); $parser->parse($part);
break; break;
case 'limit': case 'limit':
$this->parts['limit'] = trim($part); $this->parts['limit'] = trim($part);
@ -918,6 +917,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
} }
} }
} }
return end($this->_aliasMap);
} }
/** /**
* loadRoot * loadRoot
@ -959,7 +959,6 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
{ {
$oldParts = $this->parts; $oldParts = $this->parts;
$this->remove('select');
$join = $this->join; $join = $this->join;
$where = $this->where; $where = $this->where;
$having = $this->having; $having = $this->having;
@ -973,18 +972,11 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
$q .= ' '.implode(' ',$j); $q .= ' '.implode(' ',$j);
} }
$string = $this->applyInheritance(); $string = $this->applyInheritance();
if ( ! empty($string)) {
if( ! empty($where)) { $where[] = $string;
$q .= ' WHERE ' . implode(' AND ', $where);
if( ! empty($string))
$q .= ' AND (' . $string . ')';
} else {
if( ! empty($string))
$q .= ' WHERE (' . $string . ')';
} }
$q .= ( ! empty($where)) ? ' WHERE ' . implode(' AND ', $where) : '';
if( ! empty($having)) $q .= ( ! empty($having)) ? ' HAVING ' . implode(' AND ', $having): '';
$q .= ' HAVING ' . implode(' AND ',$having);
if( ! is_array($params)) { if( ! is_array($params)) {
$params = array($params); $params = array($params);
@ -992,7 +984,6 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
$params = array_merge($this->params, $params); $params = array_merge($this->params, $params);
$this->parts = $oldParts;
return (int) $this->getConnection()->fetchOne($q, $params); return (int) $this->getConnection()->fetchOne($q, $params);
} }
/** /**
@ -1136,7 +1127,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
*/ */
public function leftJoin($join) public function leftJoin($join)
{ {
return $this->getParser('from')->parse('LERT JOIN ' . $join); return $this->getParser('from')->parse('LEFT JOIN ' . $join);
} }
/** /**
* groupBy * groupBy
@ -1187,12 +1178,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
* orderBy * orderBy
* sets the ORDER BY part of the query * 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 * @return Doctrine_Query
*/ */
public function orderBy($dql) public function orderBy($orderby)
{ {
return $this->getParser('orderby')->parse($dql); return $this->getParser('orderby')->parse($orderby);
} }
/** /**
* limit * limit
@ -1203,7 +1194,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
*/ */
public function limit($limit) public function limit($limit)
{ {
return $this->getParser('limit')->parse($dql); return $this->getParser('limit')->parse($limit);
} }
/** /**
* offset * offset
@ -1212,9 +1203,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
* @param integer $offset offset to be used for paginating the query * @param integer $offset offset to be used for paginating the query
* @return Doctrine_Query * @return Doctrine_Query
*/ */
public function offset($dql) public function offset($offset)
{ {
return $this->getParser('offset')->parse($dql); return $this->getParser('offset')->parse($offset);
} }
} }