Commit f64c97d7 authored by Steve Müller's avatar Steve Müller

Merge pull request #790 from jarekj:master

[DBAL-1137] Fix infinite recursion on non-unique table/join alias in QueryBuilder

Close #790
Close #791
parents 8b848eac ab3fb95e
......@@ -1309,6 +1309,9 @@ class QueryBuilder
if (isset($this->sqlParts['join'][$fromAlias])) {
foreach ($this->sqlParts['join'][$fromAlias] as $join) {
if (array_key_exists($join['joinAlias'], $knownAliases)) {
throw QueryException::nonUniqueAlias($join['joinAlias'], array_keys($knownAliases));
}
$sql .= ' ' . strtoupper($join['joinType'])
. ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
. ' ON ' . ((string) $join['joinCondition']);
......
......@@ -38,4 +38,17 @@ class QueryException extends DBALException
"any FROM or JOIN clause table. The currently registered " .
"aliases are: " . implode(", ", $registeredAliases) . ".");
}
/**
* @param string $alias
* @param array $registeredAliases
*
* @return \Doctrine\DBAL\Query\QueryException
*/
static public function nonUniqueAlias($alias, $registeredAliases)
{
return new self("The given alias '" . $alias . "' is not unique " .
"in FROM and JOIN clause table. The currently registered " .
"aliases are: " . implode(", ", $registeredAliases) . ".");
}
}
......@@ -863,4 +863,23 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertSame(array('name' => \PDO::PARAM_STR, 'isActive' => \PDO::PARAM_BOOL), $qb->getParameterTypes());
}
/**
* @group DBAL-1137
*/
public function testJoinWithNonUniqueAliasThrowsException()
{
$qb = new QueryBuilder($this->conn);
$qb->select('a.id')
->from('table_a', 'a')
->join('a', 'table_b', 'a', 'a.fk_b = a.id');
$this->setExpectedException(
'Doctrine\DBAL\Query\QueryException',
"The given alias 'a' is not unique in FROM and JOIN clause table. The currently registered aliases are: a."
);
$qb->getSQL();
}
}
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