Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff4563f

Browse filesBrowse files
committed
PHPORM-229 Make Query\Builder return objects instead of array to match Laravel's behavior
1 parent f1ffd1b commit ff4563f
Copy full SHA for ff4563f

File tree

Expand file treeCollapse file tree

12 files changed

+194
-168
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+194
-168
lines changed

‎CHANGELOG.md

Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [5.0.0] - next
55

66
* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040)
7+
* **BREAKING CHANGE** Make Query\Builder return objects instead of array to match Laravel behavior by @GromNaN in [#3107](https://github.com/mongodb/laravel-mongodb/pull/3107)
78

89
## [4.8.0] - 2024-08-27
910

‎docs/includes/query-builder/QueryBuilderTest.php

Copy file name to clipboardExpand all lines: docs/includes/query-builder/QueryBuilderTest.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,11 @@ public function testUpsert(): void
531531

532532
$this->assertSame(2, $result);
533533

534-
$this->assertSame(119, DB::table('movies')->where('title', 'Inspector Maigret')->first()['runtime']);
535-
$this->assertSame(false, DB::table('movies')->where('title', 'Inspector Maigret')->first()['recommended']);
534+
$this->assertSame(119, DB::table('movies')->where('title', 'Inspector Maigret')->first()->runtime);
535+
$this->assertSame(false, DB::table('movies')->where('title', 'Inspector Maigret')->first()->recommended);
536536

537-
$this->assertSame(true, DB::table('movies')->where('title', 'Petit Maman')->first()['recommended']);
538-
$this->assertSame(72, DB::table('movies')->where('title', 'Petit Maman')->first()['runtime']);
537+
$this->assertSame(true, DB::table('movies')->where('title', 'Petit Maman')->first()->recommended);
538+
$this->assertSame(72, DB::table('movies')->where('title', 'Petit Maman')->first()->runtime);
539539
}
540540

541541
public function testUpdateUpsert(): void

‎src/Query/Builder.php

Copy file name to clipboardExpand all lines: src/Query/Builder.php
+36-11Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\Driver\Cursor;
2626
use Override;
2727
use RuntimeException;
28+
use stdClass;
2829

2930
use function array_fill_keys;
3031
use function array_is_list;
@@ -45,18 +46,21 @@
4546
use function func_get_args;
4647
use function func_num_args;
4748
use function get_debug_type;
49+
use function get_object_vars;
4850
use function implode;
4951
use function in_array;
5052
use function is_array;
5153
use function is_bool;
5254
use function is_callable;
5355
use function is_float;
5456
use function is_int;
57+
use function is_object;
5558
use function is_string;
5659
use function md5;
5760
use function preg_match;
5861
use function preg_quote;
5962
use function preg_replace;
63+
use function property_exists;
6064
use function serialize;
6165
use function sprintf;
6266
use function str_ends_with;
@@ -391,7 +395,7 @@ public function toMql(): array
391395
}
392396

393397
$options = [
394-
'typeMap' => ['root' => 'array', 'document' => 'array'],
398+
'typeMap' => ['root' => 'object', 'document' => 'array'],
395399
];
396400

397401
// Add custom query options
@@ -450,8 +454,7 @@ public function toMql(): array
450454
$options['projection'] = $projection;
451455
}
452456

453-
// Fix for legacy support, converts the results to arrays instead of objects.
454-
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
457+
$options['typeMap'] = ['root' => 'object', 'document' => 'array'];
455458

456459
// Add custom query options
457460
if (count($this->options)) {
@@ -516,7 +519,7 @@ public function getFresh($columns = [], $returnLazy = false)
516519
}
517520

518521
foreach ($result as &$document) {
519-
if (is_array($document)) {
522+
if (is_array($document) || is_object($document)) {
520523
$document = $this->aliasIdForResult($document);
521524
}
522525
}
@@ -1641,16 +1644,38 @@ private function aliasIdForQuery(array $values): array
16411644
return $values;
16421645
}
16431646

1644-
private function aliasIdForResult(array $values): array
1647+
/**
1648+
* @psalm-param T $values
1649+
*
1650+
* @psalm-return T
1651+
*
1652+
* @template T of array|object
1653+
*/
1654+
private function aliasIdForResult(array|object $values): array|object
16451655
{
1646-
if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) {
1647-
$values['id'] = $values['_id'];
1648-
//unset($values['_id']);
1656+
if (is_array($values)) {
1657+
if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) {
1658+
$values['id'] = $values['_id'];
1659+
//unset($values['_id']);
1660+
}
1661+
1662+
foreach ($values as $key => $value) {
1663+
if (is_array($value) || is_object($value)) {
1664+
$values[$key] = $this->aliasIdForResult($value);
1665+
}
1666+
}
16491667
}
16501668

1651-
foreach ($values as $key => $value) {
1652-
if (is_array($value)) {
1653-
$values[$key] = $this->aliasIdForResult($value);
1669+
if ($values instanceof stdClass) {
1670+
if (property_exists($values, '_id') && ! property_exists($values, 'id')) {
1671+
$values->id = $values->_id;
1672+
//unset($values->_id);
1673+
}
1674+
1675+
foreach (get_object_vars($values) as $key => $value) {
1676+
if (is_array($value) || is_object($value)) {
1677+
$values->{$key} = $this->aliasIdForResult($value);
1678+
}
16541679
}
16551680
}
16561681

‎src/Queue/Failed/MongoFailedJobProvider.php

Copy file name to clipboardExpand all lines: src/Queue/Failed/MongoFailedJobProvider.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function log($connection, $queue, $payload, $exception)
4343
*/
4444
public function all()
4545
{
46-
$all = $this->getTable()->orderBy('_id', 'desc')->get()->all();
46+
$all = $this->getTable()->orderBy('id', 'desc')->get()->all();
4747

4848
$all = array_map(function ($job) {
49-
$job['id'] = (string) $job['_id'];
49+
$job->id = (string) $job->id;
5050

51-
return (object) $job;
51+
return $job;
5252
}, $all);
5353

5454
return $all;
@@ -69,9 +69,9 @@ public function find($id)
6969
return null;
7070
}
7171

72-
$job['id'] = (string) $job['_id'];
72+
$job->id = (string) $job->id;
7373

74-
return (object) $job;
74+
return $job;
7575
}
7676

7777
/**

‎src/Queue/MongoQueue.php

Copy file name to clipboardExpand all lines: src/Queue/MongoQueue.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function releaseJobsThatHaveBeenReservedTooLong($queue)
116116
->get();
117117

118118
foreach ($reserved as $job) {
119-
$this->releaseJob($job['_id'], $job['attempts']);
119+
$this->releaseJob($job->id, $job->attempts);
120120
}
121121
}
122122

‎tests/AuthTest.php

Copy file name to clipboardExpand all lines: tests/AuthTest.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ function ($actualUser, $actualToken) use ($user, &$token) {
6161

6262
$this->assertEquals(1, DB::table('password_reset_tokens')->count());
6363
$reminder = DB::table('password_reset_tokens')->first();
64-
$this->assertEquals('john.doe@example.com', $reminder['email']);
65-
$this->assertNotNull($reminder['token']);
66-
$this->assertInstanceOf(UTCDateTime::class, $reminder['created_at']);
64+
$this->assertEquals('john.doe@example.com', $reminder->email);
65+
$this->assertNotNull($reminder->token);
66+
$this->assertInstanceOf(UTCDateTime::class, $reminder->created_at);
6767

6868
$credentials = [
6969
'email' => 'john.doe@example.com',

‎tests/Query/BuilderTest.php

Copy file name to clipboardExpand all lines: tests/Query/BuilderTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function testMql(array $expected, Closure $build, ?string $requiredMethod
4444

4545
// Operations that return a Cursor expect a "typeMap" option.
4646
if (isset($expected['find'][1])) {
47-
$expected['find'][1]['typeMap'] = ['root' => 'array', 'document' => 'array'];
47+
$expected['find'][1]['typeMap'] = ['root' => 'object', 'document' => 'array'];
4848
}
4949

5050
if (isset($expected['aggregate'][1])) {
51-
$expected['aggregate'][1]['typeMap'] = ['root' => 'array', 'document' => 'array'];
51+
$expected['aggregate'][1]['typeMap'] = ['root' => 'object', 'document' => 'array'];
5252
}
5353

5454
// Compare with assertEquals because the query can contain BSON objects.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.