Commit 22d2e79d authored by Jens Segers's avatar Jens Segers

Subqueries testing and grouping fix

parent 98302353
...@@ -79,10 +79,21 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -79,10 +79,21 @@ class Query extends \Illuminate\Database\Query\Builder {
if ($this->groups || $this->aggregate) if ($this->groups || $this->aggregate)
{ {
$pipeline = array(); $pipeline = array();
$group = array();
// Grouping // Grouping
$group = array(); if ($this->groups)
$group['_id'] = $this->groups ? $this->groups : 0; {
foreach ($this->groups as $column)
{
$group['_id'][$column] = '$' . $column;
$group[$column] = array('$last' => '$' . $column);
}
}
else
{
$group['_id'] = 0;
}
// Columns // Columns
foreach ($this->columns as $column) foreach ($this->columns as $column)
...@@ -199,24 +210,6 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -199,24 +210,6 @@ class Query extends \Illuminate\Database\Query\Builder {
return $this; return $this;
} }
/**
* Add a "group by" clause to the query.
*
* @param dynamic $columns
* @return Builder
*/
public function groupBy()
{
$groups = func_get_args();
foreach ($groups as $group)
{
$this->groups[$group] = '$' . $group;
}
return $this;
}
/** /**
* Add a where between statement to the query. * Add a where between statement to the query.
* *
......
...@@ -183,4 +183,48 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -183,4 +183,48 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(37, User::where('title', 'user')->max('age')); $this->assertEquals(37, User::where('title', 'user')->max('age'));
} }
public function testGroupBy()
{
$users = User::groupBy('title')->get();
$this->assertEquals(3, count($users));
$users = User::groupBy('age')->get();
$this->assertEquals(6, count($users));
$users = User::groupBy('age')->orderBy('age', 'desc')->get();
$this->assertEquals(37, $users[0]->age);
$this->assertEquals(35, $users[1]->age);
$this->assertEquals(33, $users[2]->age);
}
public function testSubquery()
{
$users = User::where('title', 'admin')->orWhere(function($query)
{
$query->where('name', 'Tommy Toe')
->orWhere('name', 'Error');
})
->get();
$this->assertEquals(5, count($users));
$users = User::where('title', 'user')->where(function($query)
{
$query->where('age', 35)
->orWhere('name', 'like', '%harry%');
})
->get();
$this->assertEquals(2, count($users));
$users = User::where('age', 35)->orWhere(function($query)
{
$query->where('title', 'admin')
->orWhere('name', 'Error');
})
->get();
$this->assertEquals(5, count($users));
}
} }
\ No newline at end of file
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