Commit fa2da84b authored by zYne's avatar zYne

--no commit message

--no commit message
parent 86fa70d6
...@@ -151,6 +151,10 @@ class Doctrine_Hydrate2 ...@@ -151,6 +151,10 @@ class Doctrine_Hydrate2
{ {
$this->tableAliases = $aliases; $this->tableAliases = $aliases;
} }
public function getTableAlias($componentAlias)
{
return $this->aliasHandler->getShortAlias($componentAlias);
}
/** /**
* copyAliases * copyAliases
* *
......
...@@ -140,12 +140,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -140,12 +140,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
public function processPendingFields($componentAlias) public function processPendingFields($componentAlias)
{ {
$tableAlias = $this->getTableAlias($componentAlias); $tableAlias = $this->getTableAlias($componentAlias);
$table = $this->_aliasMap[$componentAlias]['table'];
if ( ! isset($this->tables[$tableAlias])) {
throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias);
}
$table = $this->tables[$tableAlias];
if (isset($this->pendingFields[$componentAlias])) { if (isset($this->pendingFields[$componentAlias])) {
$fields = $this->pendingFields[$componentAlias]; $fields = $this->pendingFields[$componentAlias];
...@@ -642,13 +637,14 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -642,13 +637,14 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
*/ */
public function getQuery($params = array()) public function getQuery($params = array())
{ {
if(empty($this->parts["select"]) || empty($this->parts["from"])) if (empty($this->parts['select']) || empty($this->parts['from'])) {
return false; return false;
}
$needsSubQuery = false; $needsSubQuery = false;
$subquery = ''; $subquery = '';
$k = array_keys($this->tables); $k = array_keys($this->_aliasMap);
$table = $this->tables[$k[0]]; $table = $this->_aliasMap[$k[0]]['table'];
if( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) { if( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) {
$needsSubQuery = true; $needsSubQuery = true;
...@@ -661,18 +657,18 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -661,18 +657,18 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
// build the basic query // build the basic query
$str = ''; $str = '';
if($this->isDistinct()) if($this->isDistinct()) {
$str = 'DISTINCT '; $str = 'DISTINCT ';
}
$q = $this->getQueryBase(); $q = $this->getQueryBase();
$q .= array_shift($this->parts['from']);
print $q;
foreach ($this->parts['from'] as $part) {
$q .= $this->parts['from'];
foreach($this->parts['join'] as $parts) {
foreach($parts as $part) {
// preserve LEFT JOINs only if needed // preserve LEFT JOINs only if needed
if(substr($part, 0,9) === 'LEFT JOIN') { if (substr($part, 0, 9) === 'LEFT JOIN') {
$e = explode(' ', $part); $e = explode(' ', $part);
$aliases = array_merge($this->subqueryAliases, $aliases = array_merge($this->subqueryAliases,
...@@ -693,31 +689,31 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -693,31 +689,31 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
// we can always be sure that the first join condition exists // we can always be sure that the first join condition exists
$e2 = explode(' AND ', $e[1]); $e2 = explode(' AND ', $e[1]);
$part = $e[0] . ' ON ' $part = $e[0] . ' ON ' . array_shift($e2);
. array_shift($e2);
if( ! empty($e2)) { if ( ! empty($e2)) {
$parser = new Doctrine_Query_JoinCondition($this); $parser = new Doctrine_Query_JoinCondition($this);
$part .= ' AND ' . $parser->parse(implode(' AND ', $e2)); $part .= ' AND ' . $parser->parse(implode(' AND ', $e2));
} }
$q .= ' ' . $part; $q .= ' ' . $part;
} }
}
if( ! empty($this->parts['set'])) {
if ( ! empty($this->parts['set'])) {
$q .= ' SET ' . implode(', ', $this->parts['set']); $q .= ' SET ' . implode(', ', $this->parts['set']);
} }
$string = $this->applyInheritance(); $string = $this->applyInheritance();
if( ! empty($string)) if ( ! empty($string)) {
$this->parts['where'][] = '('.$string.')'; $this->parts['where'][] = '(' . $string . ')';
}
$modifyLimit = true; $modifyLimit = true;
if( ! empty($this->parts["limit"]) || ! empty($this->parts["offset"])) { if ( ! empty($this->parts["limit"]) || ! empty($this->parts["offset"])) {
if($needsSubQuery) { if($needsSubQuery) {
$subquery = $this->getLimitSubquery(); $subquery = $this->getLimitSubquery();
...@@ -750,15 +746,17 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -750,15 +746,17 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$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']);
}
// return to the previous state // return to the previous state
if( ! empty($string)) if ( ! empty($string)) {
array_pop($this->parts['where']); array_pop($this->parts['where']);
if($needsSubQuery) }
if ($needsSubQuery) {
array_shift($this->parts['where']); array_shift($this->parts['where']);
}
return $q; return $q;
} }
/** /**
...@@ -1048,21 +1046,39 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1048,21 +1046,39 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$tmp = explode(' ', $path); $tmp = explode(' ', $path);
$componentAlias = (count($tmp) > 1) ? end($tmp) : false; $componentAlias = (count($tmp) > 1) ? end($tmp) : false;
$e = preg_split("/[.:]/", $tmp[0], -1); $e = preg_split("/[.:]/", $tmp[0], -1);
$fullPath = $tmp[0];
$prevPath = '';
if (isset($this->_aliasMap[$e[0]])) { if (isset($this->_aliasMap[$e[0]])) {
$table = $this->_aliasMap[$e[0]]['table']; $table = $this->_aliasMap[$e[0]]['table'];
$parent = array_shift($e); $prevPath = $parent = array_shift($e);
} }
foreach ($e as $key => $name) { foreach ($e as $key => $name) {
// build the current component path
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
// if an alias is not given use the current path as an alias identifier
if ($prevPath !== $fullPath || ! $componentAlias) {
$componentAlias = $prevPath;
}
// load fields if necessary
if ($loadFields) {
$this->pendingFields[$componentAlias] = array('*');
}
if ( ! isset($table)) { if ( ! isset($table)) {
// process the root of the path // process the root of the path
$table = $this->loadRoot($name, $componentAlias); $table = $this->loadRoot($name, $componentAlias);
} else { } else {
$relation = $table->getRelation($name); $relation = $table->getRelation($name);
$this->_aliasMap[$componentAlias] = array('parent' => $parent, $this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
'parent' => $parent,
'relation' => $relation); 'relation' => $relation);
if( ! $relation->isOneToOne()) { if( ! $relation->isOneToOne()) {
$this->needsSubquery = true; $this->needsSubquery = true;
...@@ -1070,15 +1086,15 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1070,15 +1086,15 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$localAlias = $this->getShortAlias($parent, $table->getTableName()); $localAlias = $this->getShortAlias($parent, $table->getTableName());
$foreignAlias = $this->getShortAlias($componentAlias, $relation->getTable()->getTableName()); $foreignAlias = $this->getShortAlias($componentAlias, $relation->getTable()->getTableName());
$aliasString = $this->conn->quoteIdentifier($table->getTableName()) . ' AS ' . $localAlias; $localSql = $this->conn->quoteIdentifier($table->getTableName()) . ' ' . $localAlias;
$foreignSql = $this->conn->quoteIdentifier($relation->getTable()->getTableName()) . ' ' . $foreignAlias;
$map = $relation->getTable()->inheritanceMap; $map = $relation->getTable()->inheritanceMap;
if( ! $loadFields || ! empty($map) || $joinCondition) { if ( ! $loadFields || ! empty($map) || $joinCondition) {
$this->subqueryAliases[] = $foreignAlias; $this->subqueryAliases[] = $foreignAlias;
} }
if ($relation instanceof Doctrine_Relation_Association) { if ($relation instanceof Doctrine_Relation_Association) {
$asf = $relation->getAssociationFactory(); $asf = $relation->getAssociationFactory();
...@@ -1090,13 +1106,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1090,13 +1106,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$assocPath = $prevPath . '.' . $asf->getComponentName(); $assocPath = $prevPath . '.' . $asf->getComponentName();
if (isset($this->tableAliases[$assocPath])) { $assocAlias = $this->getShortAlias($assocPath, $asf->getTableName());
$assocAlias = $this->tableAliases[$assocPath];
} else {
$assocAlias = $this->aliasHandler->generateShortAlias($assocTableName);
}
$queryPart = 'LEFT JOIN ' . $assocTableName . ' ' . $assocAlias . ' ON ' . $foreignAlias . '.' $queryPart = 'LEFT JOIN ' . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.'
. $table->getIdentifier() . ' = ' . $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getLocal(); . $assocAlias . '.' . $relation->getLocal();
...@@ -1105,7 +1117,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1105,7 +1117,9 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
. $assocAlias . '.' . $relation->getForeign(); . $assocAlias . '.' . $relation->getForeign();
} }
$queryPart = 'LEFT JOIN ' . $aliasString . ' ON ' . $foreignAlias . '.' $this->parts['from'][] = $queryPart;
$queryPart = 'LEFT JOIN ' . $foreignSql . ' ON ' . $foreignAlias . '.'
. $relation->getTable()->getIdentifier() . ' = ' . $relation->getTable()->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getForeign() . $assocAlias . '.' . $relation->getForeign()
. $joinCondition; . $joinCondition;
...@@ -1116,7 +1130,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1116,7 +1130,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
} }
} else { } else {
$queryPart = 'LEFT JOIN ' . $aliasString $queryPart = 'LEFT JOIN ' . $localSql
. ' ON ' . $localAlias . '.' . ' ON ' . $localAlias . '.'
. $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign() . $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign()
. $joinCondition; . $joinCondition;
...@@ -1133,6 +1147,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1133,6 +1147,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
} }
} }
} }
/**
* loadRoot
*
* @param string $name
* @param string $componentAlias
*/
public function loadRoot($name, $componentAlias) public function loadRoot($name, $componentAlias)
{ {
// get the connection for the component // get the connection for the component
...@@ -1143,7 +1163,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1143,7 +1163,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$tableName = $table->getTableName(); $tableName = $table->getTableName();
// get the short alias for this table // get the short alias for this table
$tableAlias = $this->aliasHandler->getShortAlias($tableName); $tableAlias = $this->aliasHandler->getShortAlias($componentAlias, $tableName);
// quote table name // quote table name
$queryPart = $this->conn->quoteIdentifier($tableName); $queryPart = $this->conn->quoteIdentifier($tableName);
...@@ -1171,11 +1191,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1171,11 +1191,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
*/ */
public function getShortAlias($componentAlias, $tableName) public function getShortAlias($componentAlias, $tableName)
{ {
if (isset($this->tableAliases[$componentAlias])) { return $this->aliasHandler->getShortAlias($componentAlias, $tableName);
return $this->tableAliases[$componentAlias];
}
return $this->aliasHandler->getShortAlias($tableName);
} }
} }
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