From a39199f29decfaea7e150f4d15c8912e8931aecf Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 27 Jan 2007 10:50:03 +0000 Subject: [PATCH] DQL ORDER BY now supports ordering by an aggregate value --- lib/Doctrine/Hydrate.php | 5 ++++- lib/Doctrine/Query.php | 21 +++++++++++++++++++-- lib/Doctrine/Query/Orderby.php | 11 ++++++++--- tests/Query/JoinConditionTestCase.php | 4 ++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index fbd1a62d9..f635f882e 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -85,7 +85,10 @@ abstract class Doctrine_Hydrate extends Doctrine_Access protected $tableIndexes = array(); protected $pendingAggregates = array(); - + /** + * @var array $aggregateMap an array containing all aggregate aliases, keys as dql aliases + * and values as sql aliases + */ protected $aggregateMap = array(); /** * @var Doctrine_Hydrate_Alias $aliasHandler diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 385f022ce..83b4431e5 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -76,6 +76,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { * @var array $pendingFields */ private $pendingFields = array(); + /** * @var integer $type the query type * @@ -115,6 +116,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $this->isSubquery = (bool) $bool; return $this; } + /** + * getAggregateAlias + * + * @return string + */ + public function getAggregateAlias($dqlAlias) + { + if(isset($this->aggregateMap[$dqlAlias])) { + return $this->aggregateMap[$dqlAlias]; + } + + return null; + } + public function getTableStack() { return $this->tableStack; @@ -257,9 +272,11 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { } } - $this->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $tableAlias . '__' . count($this->aggregateMap); + $sqlAlias = $tableAlias . '__' . count($this->aggregateMap); - $this->aggregateMap[] = $table; + $this->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $sqlAlias; + + $this->aggregateMap[$alias] = $sqlAlias; $this->neededTables[] = $tableAlias; } } diff --git a/lib/Doctrine/Query/Orderby.php b/lib/Doctrine/Query/Orderby.php index 6435b30c6..627753b2c 100644 --- a/lib/Doctrine/Query/Orderby.php +++ b/lib/Doctrine/Query/Orderby.php @@ -60,9 +60,14 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part $r = $alias . '.' . $field; - if (isset($e[1])) { - $r .= ' '.$e[1]; - } + + } else { + $field = $this->query->getAggregateAlias($e[0]); + + $r = $field; + } + if (isset($e[1])) { + $r .= ' '.$e[1]; } $ret[] = $r; } diff --git a/tests/Query/JoinConditionTestCase.php b/tests/Query/JoinConditionTestCase.php index 4e7a35da6..823fa8203 100644 --- a/tests/Query/JoinConditionTestCase.php +++ b/tests/Query/JoinConditionTestCase.php @@ -32,6 +32,10 @@ */ class Doctrine_Query_JoinCondition_TestCase extends Doctrine_UnitTestCase { + public function prepareData() + { } + public function prepareTables() + { } public function testJoinConditionsAreSupportedForOneToManyLeftJoins() { $q = new Doctrine_Query();