Commit a39199f2 authored by zYne's avatar zYne

DQL ORDER BY now supports ordering by an aggregate value

parent 84a7fb79
...@@ -85,7 +85,10 @@ abstract class Doctrine_Hydrate extends Doctrine_Access ...@@ -85,7 +85,10 @@ abstract class Doctrine_Hydrate extends Doctrine_Access
protected $tableIndexes = array(); protected $tableIndexes = array();
protected $pendingAggregates = 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(); protected $aggregateMap = array();
/** /**
* @var Doctrine_Hydrate_Alias $aliasHandler * @var Doctrine_Hydrate_Alias $aliasHandler
......
...@@ -76,6 +76,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { ...@@ -76,6 +76,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
* @var array $pendingFields * @var array $pendingFields
*/ */
private $pendingFields = array(); private $pendingFields = array();
/** /**
* @var integer $type the query type * @var integer $type the query type
* *
...@@ -115,6 +116,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { ...@@ -115,6 +116,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$this->isSubquery = (bool) $bool; $this->isSubquery = (bool) $bool;
return $this; return $this;
} }
/**
* getAggregateAlias
*
* @return string
*/
public function getAggregateAlias($dqlAlias)
{
if(isset($this->aggregateMap[$dqlAlias])) {
return $this->aggregateMap[$dqlAlias];
}
return null;
}
public function getTableStack() public function getTableStack()
{ {
return $this->tableStack; return $this->tableStack;
...@@ -257,9 +272,11 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { ...@@ -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->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $sqlAlias;
$this->aggregateMap[] = $table; $this->aggregateMap[$alias] = $sqlAlias;
$this->neededTables[] = $tableAlias; $this->neededTables[] = $tableAlias;
} }
} }
......
...@@ -60,9 +60,14 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part ...@@ -60,9 +60,14 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part
$r = $alias . '.' . $field; $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; $ret[] = $r;
} }
......
...@@ -32,6 +32,10 @@ ...@@ -32,6 +32,10 @@
*/ */
class Doctrine_Query_JoinCondition_TestCase extends Doctrine_UnitTestCase class Doctrine_Query_JoinCondition_TestCase extends Doctrine_UnitTestCase
{ {
public function prepareData()
{ }
public function prepareTables()
{ }
public function testJoinConditionsAreSupportedForOneToManyLeftJoins() public function testJoinConditionsAreSupportedForOneToManyLeftJoins()
{ {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment