From 1a163cd48dc8b691351f0a9fee3e2286fe0af984 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 9 Feb 2013 22:40:34 +0100 Subject: [PATCH] [DDC-2019] Add support for expression in QueryBuilder#addOrderBy() --- lib/Doctrine/ORM/QueryBuilder.php | 12 +++++++----- tests/Doctrine/Tests/ORM/QueryBuilderTest.php | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 7206ec4ab..76d2e506c 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -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); } /** diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 6630bd3d6..19e0c0e95 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -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();