Skip to content

Commit

Permalink
Execute raw expressions, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Aug 15, 2013
1 parent 5479935 commit 5c438b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ These expressions will be injected directly into the query.

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**

You may easily cache the results of a query using the remember method:
Expand Down
15 changes: 15 additions & 0 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use MongoID;
use MongoRegex;
use Closure;

class Builder extends \Illuminate\Database\Query\Builder {

Expand Down Expand Up @@ -403,6 +404,20 @@ public function truncate()
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.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ public function testInArray()
$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());
}

}

0 comments on commit 5c438b1

Please sign in to comment.