Commit 85bad948 authored by Andrew Ryno's avatar Andrew Ryno

Modify the aggregation logic to handle keys for subdocuments.

parent bc686c93
...@@ -114,7 +114,16 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -114,7 +114,16 @@ class Builder extends \Illuminate\Database\Query\Builder {
// Pass other functions directly // Pass other functions directly
else else
{ {
$group[$column] = array('$' . $function => '$' . $column); // Normally this aggregate function would overwrite the
// $last group set above, but since we are modifying
// the string, we need to unset it directly.
if (isset($group[$column]))
{
unset($group[$column]);
}
$key = str_replace('.', '_', $column);
$group[$key] = array('$' . $function => '$' . $column);
} }
} }
} }
...@@ -203,7 +212,8 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -203,7 +212,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
if (isset($results[0])) if (isset($results[0]))
{ {
return $results[0][$columns[0]]; $key = str_replace('.', '_', $columns[0]);
return $results[0][$key];
} }
} }
......
...@@ -5,14 +5,14 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -5,14 +5,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin')); User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin', 'subdocument' => array('age' => 35)));
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin')); User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin', 'subdocument' => array('age' => 33)));
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user')); User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user', 'subdocument' => array('age' => 13)));
User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user')); User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user', 'subdocument' => array('age' => 37)));
User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user')); User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user', 'subdocument' => array('age' => 23)));
User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user')); User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user', 'subdocument' => array('age' => 35)));
User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user')); User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user', 'subdocument' => array('age' => 33)));
User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin')); User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin', 'subdocument' => array('age' => 35)));
User::create(array('name' => 'Error', 'age' => null, 'title' => null)); User::create(array('name' => 'Error', 'age' => null, 'title' => null));
} }
...@@ -176,6 +176,11 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -176,6 +176,11 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(30.5, User::avg('age')); $this->assertEquals(30.5, User::avg('age'));
$this->assertEquals(244, User::sum('age')); $this->assertEquals(244, User::sum('age'));
$this->assertEquals(37, User::max('subdocument.age'));
$this->assertEquals(13, User::min('subdocument.age'));
$this->assertEquals(30.5, User::avg('subdocument.age'));
$this->assertEquals(244, User::sum('subdocument.age'));
$this->assertEquals(35, User::where('title', 'admin')->max('age')); $this->assertEquals(35, User::where('title', 'admin')->max('age'));
$this->assertEquals(37, User::where('title', 'user')->max('age')); $this->assertEquals(37, User::where('title', 'user')->max('age'));
......
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