1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

[DDC-1619] Add QueryBuilder#distinct

This commit is contained in:
Benjamin Eberlei 2012-01-25 10:19:01 +01:00
parent f0a09a2d52
commit 7dae89bb02
2 changed files with 39 additions and 4 deletions

View file

@ -50,6 +50,7 @@ class QueryBuilder
* @var array The array of DQL parts collected. * @var array The array of DQL parts collected.
*/ */
private $_dqlParts = array( private $_dqlParts = array(
'distinct' => false,
'select' => array(), 'select' => array(),
'from' => array(), 'from' => array(),
'join' => array(), 'join' => array(),
@ -346,7 +347,7 @@ class QueryBuilder
* ->setParameters(array( * ->setParameters(array(
* 'user_id1' => 1, * 'user_id1' => 1,
* 'user_id2' => 2 * 'user_id2' => 2
* )); ));
* </code> * </code>
* *
* @param array $params The query parameters to set. * @param array $params The query parameters to set.
@ -503,6 +504,25 @@ class QueryBuilder
return $this->add('select', new Expr\Select($selects), false); 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. * Adds an item that is to be returned in the query result.
* *
@ -981,7 +1001,9 @@ class QueryBuilder
private function _getDQLForSelect() 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'); $fromParts = $this->getDQLPart('from');
$joinParts = $this->getDQLPart('join'); $joinParts = $this->getDQLPart('join');
@ -1090,4 +1112,4 @@ class QueryBuilder
} }
} }
} }
} }

View file

@ -732,4 +732,17 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL()); $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());
}
}