Commit 077abc6a authored by jeroendedauw's avatar jeroendedauw

Make the $alias parameter in the `from` method optional

parent 7b6331d2
......@@ -583,12 +583,12 @@ class QueryBuilder
* ->from('users', 'u')
* </code>
*
* @param string $from The table.
* @param string $alias The alias of the table.
* @param string $from The table.
* @param string|null $alias The alias of the table.
*
* @return \Doctrine\DBAL\Query\QueryBuilder This QueryBuilder instance.
* @return QueryBuilder This QueryBuilder instance.
*/
public function from($from, $alias)
public function from($from, $alias = null)
{
return $this->add('from', array(
'table' => $from,
......@@ -1113,12 +1113,19 @@ class QueryBuilder
// Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) {
$knownAliases[$from['alias']] = true;
if ( $from['alias'] === null ) {
$tableSql = $from['table'];
$tableReference = $from['table'];
}
else {
$tableSql = $from['table'] . ' ' . $from['alias'];
$tableReference = $from['alias'];
}
$fromClause = $from['table'] . ' ' . $from['alias']
. $this->getSQLForJoins($from['alias'], $knownAliases);
$knownAliases[$tableReference] = true;
$fromClauses[$from['alias']] = $fromClause;
$fromClauses[$tableReference] = $tableSql
. $this->getSQLForJoins($tableReference, $knownAliases);
}
$this->verifyAllAliasesAreKnown($knownAliases);
......
......@@ -694,4 +694,53 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertFalse($qb->getQueryParts() === $qb_clone->getQueryParts());
$this->assertFalse($qb->getParameters() === $qb_clone->getParameters());
}
public function testSimpleSelectWithoutTableAlias()
{
$qb = new QueryBuilder($this->conn);
$qb->select('id')
->from('users');
$this->assertEquals('SELECT id FROM users', (string) $qb);
}
public function testSelectWithSimpleWhereWithoutTableAlias()
{
$qb = new QueryBuilder($this->conn);
$qb->select('id', 'name')
->from('users')
->where('awesome=9001');
$this->assertEquals("SELECT id, name FROM users WHERE awesome=9001", (string) $qb);
}
public function testComplexSelectWithoutTableAliases()
{
$qb = new QueryBuilder($this->conn);
$qb->select('DISTINCT users.id')
->from('users')
->from('articles')
->innerJoin('users', 'permissions', 'p', 'p.user_id = users.id')
->innerJoin('articles', 'comments', 'c', 'c.article_id = articles.id')
->where('users.id = articles.user_id')
->andWhere('p.read = 1');
$this->assertEquals('SELECT DISTINCT users.id FROM users INNER JOIN permissions p ON p.user_id = users.id, articles INNER JOIN comments c ON c.article_id = articles.id WHERE (users.id = articles.user_id) AND (p.read = 1)', $qb->getSQL());
}
public function testComplexSelectWithSomeTableAliases()
{
$qb = new QueryBuilder($this->conn);
$qb->select('u.id')
->from('users', 'u')
->from('articles')
->innerJoin('u', 'permissions', 'p', 'p.user_id = u.id')
->innerJoin('articles', 'comments', 'c', 'c.article_id = articles.id');
$this->assertEquals('SELECT u.id FROM users u INNER JOIN permissions p ON p.user_id = u.id, articles INNER JOIN comments c ON c.article_id = articles.id', $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