Commit 5c438b18 authored by Jens Segers's avatar Jens Segers

Execute raw expressions, fixes #19

parent 5479935c
...@@ -230,6 +230,13 @@ These expressions will be injected directly into the query. ...@@ -230,6 +230,13 @@ These expressions will be injected directly into the query.
User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get(); User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get();
You can also perform raw expressions on the internal MongoCollection object, note that this will return the original response, and not a collection of models.
User::raw(function($collection)
{
return $collection->find();
});
**Query Caching** **Query Caching**
You may easily cache the results of a query using the remember method: You may easily cache the results of a query using the remember method:
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
use MongoID; use MongoID;
use MongoRegex; use MongoRegex;
use Closure;
class Builder extends \Illuminate\Database\Query\Builder { class Builder extends \Illuminate\Database\Query\Builder {
...@@ -403,6 +404,20 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -403,6 +404,20 @@ class Builder extends \Illuminate\Database\Query\Builder {
return (1 == (int) $result['ok']); return (1 == (int) $result['ok']);
} }
/**
* Create a raw database expression.
*
* @param closure $expression
* @return mixed
*/
public function raw($expression)
{
if ($expression instanceof Closure)
{
return call_user_func($expression, $this->collection);
}
}
/** /**
* Get a new instance of the query builder. * Get a new instance of the query builder.
* *
......
...@@ -86,4 +86,16 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -86,4 +86,16 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, count($items)); $this->assertEquals(1, count($items));
} }
public function testRaw()
{
DB::collection('users')->insert(array('name' => 'John Doe'));
$cursor = DB::collection('users')->raw(function($collection) {
return $collection->find();
});
$this->assertInstanceOf('MongoCursor', $cursor);
$this->assertEquals(1, $cursor->count());
}
} }
\ 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