Commit 12d74d57 authored by Wim Vandersmissen's avatar Wim Vandersmissen Committed by Benjamin Eberlei

Fix for DBAL-172

parent 22a30c9c
......@@ -946,29 +946,37 @@ class QueryBuilder
$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 clasues for validation purpose
// loop through all JOIN clauses for validation purpose
$knownAliases = array_merge($fromClauses,$joinAliases);
foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if ( ! isset($fromClauses[$fromAlias]) ) {
throw QueryException::unknownFromAlias($fromAlias, array_keys($fromClauses));
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']) : '')
......
......@@ -29,12 +29,10 @@ use Doctrine\DBAL\DBALException;
*/
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) . ".");
}
}
......@@ -556,17 +556,14 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
{
$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());
}
}
\ No newline at end of file
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