Browse Source

fix distinct problems in paginator.

Taylor Otwell 13 years ago
parent
commit
cd17761f72
2 changed files with 10 additions and 13 deletions
  1. 7 12
      laravel/database/query.php
  2. 3 1
      laravel/database/query/grammars/grammar.php

+ 7 - 12
laravel/database/query.php

@@ -626,12 +626,12 @@ class Query {
 	 * Get an aggregate value.
 	 * Get an aggregate value.
 	 *
 	 *
 	 * @param  string  $aggregator
 	 * @param  string  $aggregator
-	 * @param  string  $column
+	 * @param  array   $columns
 	 * @return mixed
 	 * @return mixed
 	 */
 	 */
-	public function aggregate($aggregator, $column)
+	public function aggregate($aggregator, $columns)
 	{
 	{
-		$this->aggregate = compact('aggregator', 'column');
+		$this->aggregate = compact('aggregator', 'columns');
 
 
 		$sql = $this->grammar->select($this);
 		$sql = $this->grammar->select($this);
 
 
@@ -660,7 +660,7 @@ class Query {
 		// we have the count.
 		// we have the count.
 		list($orderings, $this->orderings) = array($this->orderings, null);
 		list($orderings, $this->orderings) = array($this->orderings, null);
 
 
-		$page = Paginator::page($total = $this->count(), $per_page);
+		$page = Paginator::page($total = $this->count($columns), $per_page);
 
 
 		$this->orderings = $orderings;
 		$this->orderings = $orderings;
 
 
@@ -818,14 +818,9 @@ class Query {
 
 
 		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
 		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
 		{
 		{
-			if ($method == 'count')
-			{
-				return $this->aggregate(strtoupper($method), '*');
-			}
-			else
-			{
-				return $this->aggregate(strtoupper($method), $parameters[0]);
-			}
+			if (count($parameters) == 0) $parameters[0] = '*';
+
+			return $this->aggregate(strtoupper($method), (array) $parameters[0]);
 		}
 		}
 
 
 		throw new \Exception("Method [$method] is not defined on the Query class.");
 		throw new \Exception("Method [$method] is not defined on the Query class.");

+ 3 - 1
laravel/database/query/grammars/grammar.php

@@ -93,7 +93,9 @@ class Grammar extends \Laravel\Database\Grammar {
 	 */
 	 */
 	protected function aggregate(Query $query)
 	protected function aggregate(Query $query)
 	{
 	{
-		$column = $this->wrap($query->aggregate['column']);
+		$column = $this->columnize($query->aggregate['columns']);
+
+		if ($query->distinct and $column !== '*') $column = 'DISTINCT '.$column;
 
 
 		return 'SELECT '.$query->aggregate['aggregator'].'('.$column.')';
 		return 'SELECT '.$query->aggregate['aggregator'].'('.$column.')';
 	}
 	}