1
0
mirror of synced 2025-01-22 00:01:40 +03:00

[DDC-2019] Add support for expression in QueryBuilder#addOrderBy()

This commit is contained in:
Alexander 2013-02-09 22:40:34 +01:00
parent 9bf501dd25
commit 1a163cd48d
2 changed files with 18 additions and 5 deletions

View File

@ -1047,8 +1047,8 @@ class QueryBuilder
* Specifies an ordering for the query results.
* Replaces any previously specified orderings, if any.
*
* @param string $sort The ordering expression.
* @param string $order The ordering direction.
* @param string|Expr\OrderBy $sort The ordering expression.
* @param string $order The ordering direction.
*
* @return QueryBuilder This QueryBuilder instance.
*/
@ -1062,14 +1062,16 @@ class QueryBuilder
/**
* Adds an ordering to the query results.
*
* @param string $sort The ordering expression.
* @param string $order The ordering direction.
* @param string|Expr\OrderBy $sort The ordering expression.
* @param string $order The ordering direction.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public function addOrderBy($sort, $order = null)
{
return $this->add('orderBy', new Expr\OrderBy($sort, $order), true);
$orderBy = ($sort instanceof Expr\OrderBy) ? $sort : new Expr\OrderBy($sort, $order);
return $this->add('orderBy', $orderBy, true);
}
/**

View File

@ -375,6 +375,17 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
$this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
}
public function testAddOrderByWithExpression()
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->orderBy('u.username', 'ASC')
->addOrderBy($qb->expr()->desc('u.username'));
$this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
}
public function testAddCriteriaWhere()
{
$qb = $this->_em->createQueryBuilder();