diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 48b2dae1e..633da7820 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -147,10 +147,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { case 'COUNT': case 'AVG': $reference = substr($func, ($pos + 1), -1); + $e2 = explode(' ', $reference); + + $distinct = ''; + if(count($e2) > 1) { + if(strtoupper($e2[0]) == 'DISTINCT') + $distinct = 'DISTINCT '; + + $reference = $e2[1]; + } $parts = explode('.', $reference); + $alias = (isset($e[1])) ? $e[1] : $name; - $this->pendingAggregates[$parts[0]][] = array($alias, $parts[1]); + $this->pendingAggregates[$parts[0]][] = array($alias, $parts[1], $distinct); break; default: throw new Doctrine_Query_Exception('Unknown aggregate function '.$name); @@ -167,9 +177,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $table = $this->components[$componentPath]; foreach($this->pendingAggregates[$componentAlias] as $args) { - list($name, $arg) = $args; + list($name, $arg, $distinct) = $args; - $this->parts["select"][] = $name . '(' . $tableAlias . '.' . $arg . ') AS ' . $tableAlias . '__' . count($this->aggregateMap); + $this->parts["select"][] = $name . '(' . $distinct . $tableAlias . '.' . $arg . ') AS ' . $tableAlias . '__' . count($this->aggregateMap); $this->aggregateMap[] = $table; } diff --git a/lib/Doctrine/Schema/Relation.php b/lib/Doctrine/Schema/Relation.php index 8be36972b..041e29681 100644 --- a/lib/Doctrine/Schema/Relation.php +++ b/lib/Doctrine/Schema/Relation.php @@ -104,17 +104,14 @@ class Doctrine_Schema_Relation extends Doctrine_Schema_Object { $this->referencedColumn = $referencedColumn; } /** - * - * @return - * @access public + * @return string */ public function __toString( ) { - return "Relation between '".$this->referencingColumn."' and '".$this->referencedTable."'.'".$this->referencingColumn."'"; + return "Relation between '".$this->referencingColumn."' and '".$this->referencedTable."'.'".$this->referencingColumn."'"; } /** * * @return bool - * @access public */ public function isValid( ) { diff --git a/tests/QuerySelectTestCase.php b/tests/QuerySelectTestCase.php index 849968a38..ee036e8b1 100644 --- a/tests/QuerySelectTestCase.php +++ b/tests/QuerySelectTestCase.php @@ -1,6 +1,13 @@ parseQuery('SELECT COUNT(DISTINCT u.name) FROM User u'); + + $this->assertEqual($q->getQuery(), 'SELECT COUNT(DISTINCT e.name) AS e__0 FROM entity e WHERE (e.type = 0)'); + } public function testAggregateFunction() { $q = new Doctrine_Query(); @@ -9,6 +16,7 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)'); } + public function testMultipleAggregateFunctions() { $q = new Doctrine_Query();