Skip to content
Browse files

Fix for DBAL-172

  • Loading branch information...
1 parent b1562a5 commit 70cb96791a4dee7404c523cfefc433a05eff151d Wim Vandersmissen committed Jul 18, 2012
View
37 lib/Doctrine/DBAL/Query/QueryBuilder.php
@@ -946,29 +946,36 @@ private function getSQLForSelect()
$query = 'SELECT ' . implode(', ', $this->sqlParts['select']) . ' FROM ';
$fromClauses = array();
-
+ $joinsPending = true;
+ $joinAliases = array();
+
// Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) {
$fromClause = $from['table'] . ' ' . $from['alias'];
- if (isset($this->sqlParts['join'][$from['alias']])) {
- foreach ($this->sqlParts['join'][$from['alias']] as $join) {
- $fromClause .= ' ' . strtoupper($join['joinType'])
- . ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
- . ' ON ' . ((string) $join['joinCondition']);
- }
- }
-
+ if ($joinsPending && isset($this->sqlParts['join'][$from['alias']])) {
+ foreach ($this->sqlParts['join'] as $joins) {
+ foreach ($joins as $join) {
+ $fromClause .= ' ' . strtoupper($join['joinType'])
+ . ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
+ . ' ON ' . ((string) $join['joinCondition']);
+ $joinAliases[$join['joinAlias']] = true;
+ }
+ }
+ $joinsPending = false;
+ }
+
$fromClauses[$from['alias']] = $fromClause;
}
// loop through all JOIN clauses for validation purpose
- foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
- if ( ! isset($fromClauses[$fromAlias]) ) {
- throw QueryException::unknownFromAlias($fromAlias, array_keys($fromClauses));
- }
- }
-
+ $knownAliases = array_merge($fromClauses,$joinAliases);
+ foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
+ if ( ! isset($knownAliases[$fromAlias]) ) {
+ throw QueryException::unknownAlias($fromAlias, array_keys($knownAliases));
+ }
+ }
+
$query .= implode(', ', $fromClauses)
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '')
. ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '')
View
8 lib/Doctrine/DBAL/Query/QueryException.php
@@ -29,12 +29,10 @@
*/
class QueryException extends DBALException
{
- static public function unknownFromAlias($alias, $registeredAliases)
+ static public function unknownAlias($alias, $registeredAliases)
{
return new self("The given alias '" . $alias . "' is not part of " .
- "any FROM clause table. The currently registered FROM-clause " .
- "aliases are: " . implode(", ", $registeredAliases) . ". Join clauses " .
- "are bound to from clauses to provide support for mixing of multiple " .
- "from and join clauses.");
+ "any FROM or JOIN clause table. The currently registered " .
+ "aliases are: " . implode(", ", $registeredAliases) . ".");
}
}
View
19 tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
@@ -556,17 +556,14 @@ public function testReferenceJoinFromJoin()
{
$qb = new QueryBuilder($this->conn);
- $qb->select("l.id", "mdsh.xcode", "mdso.xcode")
- ->from("location_tree", "l")
- ->join("l", "location_tree_pos", "p", "l.id = p.tree_id")
- ->rightJoin("l", "hotel", "h", "h.location_id = l.id")
- ->leftJoin("l", "offer_location", "ol", "l.id=ol.location_id")
- ->leftJoin("ol", "mds_offer", "mdso", "ol.offer_id = mdso.offer_id")
- ->leftJoin("h", "mds_hotel", "mdsh", "h.id = mdsh.hotel_id")
- ->where("p.parent_id IN (:ids)")
- ->andWhere("(mdso.xcode IS NOT NULL OR mdsh.xcode IS NOT NULL)");
-
- $this->setExpectedException('Doctrine\DBAL\Query\QueryException', "The given alias 'ol' is not part of any FROM clause table. The currently registered FROM-clause aliases are: l");
+ $qb->select('COUNT(DISTINCT news.id)')
+ ->from('cb_newspages', 'news')
+ ->innerJoin('news', 'nodeversion', 'nv', 'nv.refId = news.id AND nv.refEntityname=\'News\'')
+ ->innerJoin('invalid', 'nodetranslation', 'nt', 'nv.nodetranslation = nt.id')
+ ->innerJoin('nt', 'node', 'n', 'nt.node = n.id')
+ ->where('nt.lang = :lang AND n.deleted != 1');
+
+ $this->setExpectedException('Doctrine\DBAL\Query\QueryException', "The given alias 'invalid' is not part of any FROM or JOIN clause table. The currently registered aliases are: news, nv, nt, n.");
$this->assertEquals('', $qb->getSQL());
}
}

0 comments on commit 70cb967

Please sign in to comment.
Something went wrong with that request. Please try again.