Skip to content

Commit

Permalink
Merge branch 'DDC-1619'
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jan 25, 2012
2 parents f0a09a2 + 7dae89b commit 35fc3c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
28 changes: 25 additions & 3 deletions lib/Doctrine/ORM/QueryBuilder.php
Expand Up @@ -50,6 +50,7 @@ class QueryBuilder
* @var array The array of DQL parts collected.
*/
private $_dqlParts = array(
'distinct' => false,
'select' => array(),
'from' => array(),
'join' => array(),
Expand Down Expand Up @@ -346,7 +347,7 @@ public function setParameter($key, $value, $type = null)
* ->setParameters(array(
* 'user_id1' => 1,
* 'user_id2' => 2
* ));
));
* </code>
*
* @param array $params The query parameters to set.
Expand Down Expand Up @@ -503,6 +504,25 @@ public function select($select = null)
return $this->add('select', new Expr\Select($selects), false);
}

/**
* Add a DISTINCT flag to this query.
*
* <code>
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->distinct()
* ->from('User', 'u');
* </code>
*
* @param bool
* @return QueryBuilder
*/
public function distinct($flag = true)
{
$this->_dqlParts['distinct'] = (bool) $flag;
return $this;
}

/**
* Adds an item that is to be returned in the query result.
*
Expand Down Expand Up @@ -981,7 +1001,9 @@ private function _getDQLForUpdate()

private function _getDQLForSelect()
{
$dql = 'SELECT' . $this->_getReducedDQLQueryPart('select', array('pre' => ' ', 'separator' => ', '));
$dql = 'SELECT'
. ($this->_dqlParts['distinct']===true ? ' DISTINCT' : '')
. $this->_getReducedDQLQueryPart('select', array('pre' => ' ', 'separator' => ', '));

$fromParts = $this->getDQLPart('from');
$joinParts = $this->getDQLPart('join');
Expand Down Expand Up @@ -1090,4 +1112,4 @@ public function __clone()
}
}
}
}
}
15 changes: 14 additions & 1 deletion tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Expand Up @@ -732,4 +732,17 @@ public function testAddFromString()

$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
}
}

/**
* @group DDC-1619
*/
public function testAddDistinct()
{
$qb = $this->_em->createQueryBuilder()
->select('u')
->distinct()
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');

$this->assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
}
}

0 comments on commit 35fc3c0

Please sign in to comment.