diff --git a/src/Jenssegers/Mongodb/Builder.php b/src/Jenssegers/Mongodb/Builder.php index 2ca16d869..62c3defd7 100644 --- a/src/Jenssegers/Mongodb/Builder.php +++ b/src/Jenssegers/Mongodb/Builder.php @@ -114,7 +114,16 @@ public function getFresh($columns = array('*')) // 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 @@ public function aggregate($function, $columns = array('*')) if (isset($results[0])) { - return $results[0][$columns[0]]; + $key = str_replace('.', '_', $columns[0]); + return $results[0][$key]; } } @@ -270,7 +280,7 @@ public function insert(array $values) { // 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 @@ protected function performUpdate($query) /** * Convert a key to MongoID if needed - * + * * @param mixed $id * @return mixed */ @@ -548,7 +558,7 @@ protected function compileWheres() // 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') diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b4a8c1ced..09013f50b 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -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 @@ public function testAggregates() $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')); }