Skip to content

Modify the aggregation logic to handle keys for subdocuments. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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];
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -522,7 +532,7 @@ protected function performUpdate($query)

/**
* Convert a key to MongoID if needed
*
*
* @param mixed $id
* @return mixed
*/
Expand All @@ -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')
Expand Down
23 changes: 14 additions & 9 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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'));
}
Expand Down