Skip to content

Commit 5b7f335

Browse files
committed
Improve raw expressions on model classes
1 parent c691969 commit 5b7f335

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,16 +447,23 @@ These expressions will be injected directly into the query.
447447

448448
User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get();
449449

450-
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.
450+
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.
451451

452-
User::raw(function($collection)
452+
// Returns a collection of User models.
453+
$models = User::raw(function($collection)
453454
{
454455
return $collection->find();
455456
});
456457

457-
Or you can access the internal MongoCollection object directly:
458+
// Returns the original MongoCursor.
459+
$cursor = DB::collection('users')->raw(function($collection)
460+
{
461+
return $collection->find();
462+
});
463+
464+
Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:
458465

459-
User::raw()->find();
466+
$model = User::raw()->findOne(array('age' => array('$lt' => 18)));
460467

461468
The MongoClient and MongoDB objects can be accessed like this:
462469

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Jenssegers\Mongodb\Eloquent;
22

3+
use MongoCursor;
4+
35
class Builder extends \Illuminate\Database\Eloquent\Builder {
46

57
/**
@@ -9,7 +11,53 @@ class Builder extends \Illuminate\Database\Eloquent\Builder {
911
*/
1012
protected $passthru = array(
1113
'toSql', 'lists', 'insert', 'insertGetId', 'pluck',
12-
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull', 'raw'
14+
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull'
1315
);
1416

17+
/**
18+
* Create a raw database expression.
19+
*
20+
* @param closure $expression
21+
* @return mixed
22+
*/
23+
public function raw($expression = null)
24+
{
25+
// Get raw results from the query builder.
26+
$results = $this->query->raw($expression);
27+
28+
$connection = $this->model->getConnectionName();
29+
30+
// Convert MongoCursor results to a collection of models.
31+
if ($results instanceof MongoCursor)
32+
{
33+
$results = iterator_to_array($results, false);
34+
35+
$models = array();
36+
37+
// Once we have the results, we can spin through them and instantiate a fresh
38+
// model instance for each records we retrieved from the database. We will
39+
// also set the proper connection name for the model after we create it.
40+
foreach ($results as $result)
41+
{
42+
$models[] = $model = $this->model->newFromBuilder($result);
43+
44+
$model->setConnection($connection);
45+
}
46+
47+
return $this->model->newCollection($models);
48+
}
49+
50+
// The result is a single object.
51+
else if (is_array($results) and array_key_exists('_id', $results))
52+
{
53+
$model = $this->model->newFromBuilder($results);
54+
55+
$model->setConnection($connection);
56+
57+
return $model;
58+
}
59+
60+
return $results;
61+
}
62+
1563
}

tests/ModelTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,9 @@ public function testNoDocument()
164164
$this->assertEquals(null, $item);
165165
}
166166

167-
/**
168-
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
169-
*/
170167
public function testFindOrfail()
171168
{
169+
$this->setExpectedException('Illuminate\Database\Eloquent\ModelNotFoundException');
172170
User::findOrfail('51c33d8981fec6813e00000a');
173171
}
174172

@@ -344,4 +342,36 @@ public function testPushPull()
344342
$this->assertEquals(1, count($user->tags));
345343
}
346344

345+
public function testRaw()
346+
{
347+
User::create(array('name' => 'John Doe', 'age' => 35));
348+
User::create(array('name' => 'Jane Doe', 'age' => 35));
349+
User::create(array('name' => 'Harry Hoe', 'age' => 15));
350+
351+
$users = User::raw(function($collection)
352+
{
353+
return $collection->find(array('age' => 35));
354+
});
355+
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
356+
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $users[0]);
357+
358+
$user = User::raw(function($collection)
359+
{
360+
return $collection->findOne(array('age' => 35));
361+
});
362+
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
363+
364+
$count = User::raw(function($collection)
365+
{
366+
return $collection->count();
367+
});
368+
$this->assertEquals(3, $count);
369+
370+
$result = User::raw(function($collection)
371+
{
372+
return $collection->insert(array('name' => 'Yvonne Yoe', 'age' => 35));
373+
});
374+
$this->assertTrue(is_array($result));
375+
}
376+
347377
}

tests/QueryBuilderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public function testRaw()
185185
array('name' => 'John Doe', 'age' => 25)
186186
));
187187

188-
$cursor = DB::collection('users')->raw(function($collection) {
188+
$cursor = DB::collection('users')->raw(function($collection)
189+
{
189190
return $collection->find(array('age' => 20));
190191
});
191192

tests/QueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function testSubquery()
232232
$this->assertEquals(5, count($users));
233233
}
234234

235-
public function testRaw()
235+
public function testWhereRaw()
236236
{
237237
$where = array('age' => array('$gt' => 30, '$lt' => 40));
238238
$users = User::whereRaw($where)->get();

0 commit comments

Comments
 (0)