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

Sign up
Appearance settings

Commit cad2508

Browse files
committed
PHPORM-229 Make Query\Builder return objects instead of array to match Laravel's behavior
1 parent fbed3dc commit cad2508

File tree

11 files changed

+191
-168
lines changed

11 files changed

+191
-168
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,11 @@ public function testUpsert(): void
519519

520520
$this->assertSame(2, $result);
521521

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

525-
$this->assertSame(true, DB::table('movies')->where('title', 'Petit Maman')->first()['recommended']);
526-
$this->assertSame(72, DB::table('movies')->where('title', 'Petit Maman')->first()['runtime']);
525+
$this->assertSame(true, DB::table('movies')->where('title', 'Petit Maman')->first()->recommended);
526+
$this->assertSame(72, DB::table('movies')->where('title', 'Petit Maman')->first()->runtime);
527527
}
528528

529529
public function testUpdateUpsert(): void

‎src/Query/Builder.php‎

Lines changed: 34 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
}
@@ -1636,16 +1639,36 @@ private function aliasIdForQuery(array $values): array
16361639
return $values;
16371640
}
16381641

1639-
private function aliasIdForResult(array $values): array
1642+
/**
1643+
* @template T of array|object
1644+
* @psalm-param T $values
1645+
* @psalm-return T
1646+
*/
1647+
private function aliasIdForResult(array|object $values): array|object
16401648
{
1641-
if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) {
1642-
$values['id'] = $values['_id'];
1643-
//unset($values['_id']);
1649+
if (is_array($values)) {
1650+
if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) {
1651+
$values['id'] = $values['_id'];
1652+
//unset($values['_id']);
1653+
}
1654+
1655+
foreach ($values as $key => $value) {
1656+
if (is_array($value) || is_object($value)) {
1657+
$values[$key] = $this->aliasIdForResult($value);
1658+
}
1659+
}
16441660
}
16451661

1646-
foreach ($values as $key => $value) {
1647-
if (is_array($value)) {
1648-
$values[$key] = $this->aliasIdForResult($value);
1662+
if ($values instanceof stdClass) {
1663+
if (property_exists($values, '_id') && ! property_exists($values, 'id')) {
1664+
$values->id = $values->_id;
1665+
//unset($values->_id);
1666+
}
1667+
1668+
foreach (get_object_vars($values) as $key => $value) {
1669+
if (is_array($value) || is_object($value)) {
1670+
$values->{$key} = $this->aliasIdForResult($value);
1671+
}
16491672
}
16501673
}
16511674

‎src/Queue/Failed/MongoFailedJobProvider.php‎

Lines 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‎

Lines 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‎

Lines 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‎

Lines 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)

AltStyle によって変換されたページ (->オリジナル) /