Skip to content

Commit 07a44cc

Browse files
committed
PHPORM-239 Apply result transformation to raw() queries
1 parent ce97048 commit 07a44cc

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/Eloquent/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace MongoDB\Laravel\Eloquent;
66

77
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8-
use MongoDB\Driver\Cursor;
8+
use MongoDB\Driver\CursorInterface;
99
use MongoDB\Driver\Exception\WriteException;
1010
use MongoDB\Laravel\Connection;
1111
use MongoDB\Laravel\Helpers\QueriesRelationships;
@@ -177,7 +177,7 @@ public function raw($value = null)
177177
$results = $this->query->raw($value);
178178

179179
// Convert MongoCursor results to a collection of models.
180-
if ($results instanceof Cursor) {
180+
if ($results instanceof CursorInterface) {
181181
$results = iterator_to_array($results, false);
182182

183183
return $this->model->hydrate($results);

src/Query/Builder.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\BSON\UTCDateTime;
2626
use MongoDB\Builder\Stage\FluentFactoryTrait;
2727
use MongoDB\Driver\Cursor;
28+
use MongoDB\Driver\CursorInterface;
2829
use Override;
2930
use RuntimeException;
3031
use stdClass;
@@ -934,7 +935,17 @@ public function raw($value = null)
934935
{
935936
// Execute the closure on the mongodb collection
936937
if ($value instanceof Closure) {
937-
return call_user_func($value, $this->collection);
938+
$results = call_user_func($value, $this->collection);
939+
940+
if ($results instanceof CursorInterface) {
941+
$results = $results->toArray();
942+
}
943+
944+
if (is_array($results) || is_object($results)) {
945+
$results = $this->aliasIdForResult($results);
946+
}
947+
948+
return $results;
938949
}
939950

940951
// Create an expression for the given value

tests/QueryBuilderTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use MongoDB\BSON\Regex;
1919
use MongoDB\BSON\UTCDateTime;
2020
use MongoDB\Collection;
21-
use MongoDB\Driver\Cursor;
2221
use MongoDB\Driver\Monitoring\CommandFailedEvent;
2322
use MongoDB\Driver\Monitoring\CommandStartedEvent;
2423
use MongoDB\Driver\Monitoring\CommandSubscriber;
@@ -314,12 +313,16 @@ public function testRaw()
314313
['name' => 'John Doe', 'age' => 25],
315314
]);
316315

317-
$cursor = DB::table('users')->raw(function ($collection) {
318-
return $collection->find(['age' => 20]);
316+
$results = DB::table('users')->raw(function ($collection) {
317+
return $collection->find(['age' => 20], ['typeMap' => ['root' => 'array', 'document' => 'array']]);
319318
});
320319

321-
$this->assertInstanceOf(Cursor::class, $cursor);
322-
$this->assertCount(1, $cursor->toArray());
320+
$this->assertIsArray($results);
321+
$this->assertCount(1, $results);
322+
$this->assertArrayNotHasKey('_id', $results[0]);
323+
$this->assertArrayHasKey('id', $results[0]);
324+
$this->assertInstanceOf(ObjectId::class, $results[0]['id']);
325+
$this->assertSame(20, $results[0]['age']);
323326

324327
$collection = DB::table('users')->raw();
325328
$this->assertInstanceOf(Collection::class, $collection);

0 commit comments

Comments
 (0)