Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 1a163cd48dc8b691351f0a9fee3e2286fe0af984 1 parent 9bf501d
@asm89 asm89 authored
View
12 lib/Doctrine/ORM/QueryBuilder.php
@@ -1047,8 +1047,8 @@ public function orHaving($having)
* 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 @@ public function orderBy($sort, $order = null)
/**
* 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
11 tests/Doctrine/Tests/ORM/QueryBuilderTest.php
@@ -375,6 +375,17 @@ public function testAddOrderBy()
$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();
Please sign in to comment.
Something went wrong with that request. Please try again.