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 {
// Pass other functions directly
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 {
if (isset($results[0]))
{
return $results[0][$columns[0]];
$key = str_replace('.', '_', $columns[0]);
return $results[0][$key];
}
}
......@@ -270,7 +280,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
{
// As soon as we find a value that is not an array we assume the user is
// inserting a single document.
if (!is_array($value))
if (!is_array($value))
{
$batch = false; break;
}
......@@ -522,7 +532,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
/**
* Convert a key to MongoID if needed
*
*
* @param mixed $id
* @return mixed
*/
......@@ -548,7 +558,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
// The new list of compiled wheres
$wheres = array();
foreach ($this->wheres as $i => &$where)
foreach ($this->wheres as $i => &$where)
{
// Convert id's
if (isset($where['column']) && $where['column'] == '_id')
......
......@@ -5,14 +5,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass()
{
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user'));
User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user'));
User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user'));
User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user'));
User::create(array('name' => 'Yvonne Yoe', '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', 'subdocument' => array('age' => 33)));
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user', 'subdocument' => array('age' => 13)));
User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user', 'subdocument' => array('age' => 37)));
User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user', 'subdocument' => array('age' => 23)));
User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user', 'subdocument' => array('age' => 35)));
User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user', 'subdocument' => array('age' => 33)));
User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin', 'subdocument' => array('age' => 35)));
User::create(array('name' => 'Error', 'age' => null, 'title' => null));
}
......@@ -176,9 +176,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(30.5, User::avg('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(37, User::where('title', 'user')->max('age'));
$this->assertEquals(33, User::where('title', 'admin')->min('age'));
$this->assertEquals(13, User::where('title', 'user')->min('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