diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 382e9b946..b9042fa61 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -855,6 +855,9 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable foreach ($this->parts['orderby'] as $part) { $part = trim($part); + $e = Doctrine_Tokenizer::bracketExplode($part, ' '); + $part = trim($e[0]); + $aggregate = false; foreach ($this->parts['select'] as $select) { diff --git a/tests/Query/MysqlSubqueryTestCase.php b/tests/Query/MysqlSubqueryTestCase.php index 0e94c3fd0..144642de8 100644 --- a/tests/Query/MysqlSubqueryTestCase.php +++ b/tests/Query/MysqlSubqueryTestCase.php @@ -38,7 +38,7 @@ class Doctrine_Query_MysqlSubquery_TestCase extends Doctrine_UnitTestCase $this->conn = Doctrine_Manager::getInstance()->openConnection($this->dbh); } - public function testGetLimitSubqueryWithOrderByOnAggregateValues() + public function testGetLimitSubquerSupportsOrderByWithAggregateValues() { $q = new Doctrine_Query(); $q->select('u.name, COUNT(DISTINCT a.id) num_albums'); @@ -53,8 +53,22 @@ class Doctrine_Query_MysqlSubquery_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0 LIMIT 5'); } + public function testGetLimitSubquerySupportsOrderByWithAggregateValuesAndDescKeyword() + { + $q = new Doctrine_Query(); + $q->select('u.name, COUNT(DISTINCT a.id) num_albums'); + $q->from('User u, u.Album a'); + $q->orderby('num_albums DESC, u.name'); + $q->groupby('u.id'); + $q->limit(5); - public function testGetLimitSubqueryWithOrderByOnAggregateValuesAndColumns() + $q->execute(); + + $this->dbh->pop(); + + $this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0 DESC, e2.name LIMIT 5'); + } + public function testGetLimitSubquerySupportsOrderByWithAggregateValuesAndColumns() { $q = new Doctrine_Query(); $q->select('u.name, COUNT(DISTINCT a.id) num_albums');