diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index de3fbe72d..76aa5bc3d 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -47,6 +47,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable * query object */ protected $isSubquery; + + protected $isLimitSubqueryUsed = false; protected $neededTables = array(); /** @@ -128,6 +130,15 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable { return $this->_enumParams; } + /** + * limitSubqueryUsed + * + * @return boolean + */ + public function isLimitSubqueryUsed() + { + return $this->isLimitSubqueryUsed; + } /** * convertEnums * convert enum parameters to their integer equivalents @@ -564,6 +575,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $rootAlias = key($this->_aliasMap); if ( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) { + $this->isLimitSubqueryUsed = true; $needsSubQuery = true; } @@ -702,7 +714,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable continue; } - if($this->aliasHandler->hasAliasFor($part)) { + if($this->aliasHandler->hasAlias($part)) { $parts[$k] = $this->aliasHandler->generateNewAlias($part); } @@ -895,6 +907,11 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } else { $componentAlias = $prevPath; } + + // if the current alias already exists, skip it + if (isset($this->_aliasMap[$componentAlias])) { + continue; + } if ( ! isset($table)) { // process the root of the path