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

Improve raw expressions on model classes

parent c6919690
...@@ -447,16 +447,23 @@ These expressions will be injected directly into the query. ...@@ -447,16 +447,23 @@ 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. You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response.
User::raw(function($collection) // Returns a collection of User models.
$models = User::raw(function($collection)
{ {
return $collection->find(); return $collection->find();
}); });
Or you can access the internal MongoCollection object directly: // Returns the original MongoCursor.
$cursor = DB::collection('users')->raw(function($collection)
{
return $collection->find();
});
Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:
User::raw()->find(); $model = User::raw()->findOne(array('age' => array('$lt' => 18)));
The MongoClient and MongoDB objects can be accessed like this: The MongoClient and MongoDB objects can be accessed like this:
......
<?php namespace Jenssegers\Mongodb\Eloquent; <?php namespace Jenssegers\Mongodb\Eloquent;
use MongoCursor;
class Builder extends \Illuminate\Database\Eloquent\Builder { class Builder extends \Illuminate\Database\Eloquent\Builder {
/** /**
...@@ -9,7 +11,53 @@ class Builder extends \Illuminate\Database\Eloquent\Builder { ...@@ -9,7 +11,53 @@ class Builder extends \Illuminate\Database\Eloquent\Builder {
*/ */
protected $passthru = array( protected $passthru = array(
'toSql', 'lists', 'insert', 'insertGetId', 'pluck', 'toSql', 'lists', 'insert', 'insertGetId', 'pluck',
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull', 'raw' 'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull'
); );
/**
* Create a raw database expression.
*
* @param closure $expression
* @return mixed
*/
public function raw($expression = null)
{
// Get raw results from the query builder.
$results = $this->query->raw($expression);
$connection = $this->model->getConnectionName();
// Convert MongoCursor results to a collection of models.
if ($results instanceof MongoCursor)
{
$results = iterator_to_array($results, false);
$models = array();
// Once we have the results, we can spin through them and instantiate a fresh
// model instance for each records we retrieved from the database. We will
// also set the proper connection name for the model after we create it.
foreach ($results as $result)
{
$models[] = $model = $this->model->newFromBuilder($result);
$model->setConnection($connection);
}
return $this->model->newCollection($models);
}
// The result is a single object.
else if (is_array($results) and array_key_exists('_id', $results))
{
$model = $this->model->newFromBuilder($results);
$model->setConnection($connection);
return $model;
}
return $results;
}
} }
...@@ -164,11 +164,9 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -164,11 +164,9 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(null, $item); $this->assertEquals(null, $item);
} }
/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrfail() public function testFindOrfail()
{ {
$this->setExpectedException('Illuminate\Database\Eloquent\ModelNotFoundException');
User::findOrfail('51c33d8981fec6813e00000a'); User::findOrfail('51c33d8981fec6813e00000a');
} }
...@@ -344,4 +342,36 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -344,4 +342,36 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, count($user->tags)); $this->assertEquals(1, count($user->tags));
} }
public function testRaw()
{
User::create(array('name' => 'John Doe', 'age' => 35));
User::create(array('name' => 'Jane Doe', 'age' => 35));
User::create(array('name' => 'Harry Hoe', 'age' => 15));
$users = User::raw(function($collection)
{
return $collection->find(array('age' => 35));
});
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $users[0]);
$user = User::raw(function($collection)
{
return $collection->findOne(array('age' => 35));
});
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$count = User::raw(function($collection)
{
return $collection->count();
});
$this->assertEquals(3, $count);
$result = User::raw(function($collection)
{
return $collection->insert(array('name' => 'Yvonne Yoe', 'age' => 35));
});
$this->assertTrue(is_array($result));
}
} }
...@@ -185,7 +185,8 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -185,7 +185,8 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
array('name' => 'John Doe', 'age' => 25) array('name' => 'John Doe', 'age' => 25)
)); ));
$cursor = DB::collection('users')->raw(function($collection) { $cursor = DB::collection('users')->raw(function($collection)
{
return $collection->find(array('age' => 20)); return $collection->find(array('age' => 20));
}); });
......
...@@ -232,7 +232,7 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -232,7 +232,7 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(5, count($users)); $this->assertEquals(5, count($users));
} }
public function testRaw() public function testWhereRaw()
{ {
$where = array('age' => array('$gt' => 30, '$lt' => 40)); $where = array('age' => array('$gt' => 30, '$lt' => 40));
$users = User::whereRaw($where)->get(); $users = User::whereRaw($where)->get();
......
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