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 6efb2a4

Browse filesBrowse files
committed
Recursively replace .id with ._id
1 parent 6f0117d commit 6efb2a4
Copy full SHA for 6efb2a4

File tree

Expand file treeCollapse file tree

3 files changed

+71
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+71
-1
lines changed

‎src/Query/Builder.php

Copy file name to clipboardExpand all lines: src/Query/Builder.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ public function toMql(): array
298298
}
299299

300300
$wheres = $this->compileWheres();
301+
$wheres = $this->aliasIdForQuery($wheres);
301302

302303
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
303304
if ($this->groups || $this->aggregate) {
@@ -1634,13 +1635,33 @@ private function aliasIdForQuery(array $values): array
16341635
unset($values['id']);
16351636
}
16361637

1638+
foreach ($values as $key => $value) {
1639+
if (is_string($key) && str_ends_with($key, '.id')) {
1640+
$values[substr($key, 0, -3) . '._id'] = $value;
1641+
unset($values[$key]);
1642+
}
1643+
}
1644+
1645+
foreach ($values as &$value) {
1646+
if (is_array($value)) {
1647+
$value = $this->aliasIdForQuery($value);
1648+
}
1649+
}
1650+
16371651
return $values;
16381652
}
16391653

16401654
private function aliasIdForResult(array $values): array
16411655
{
16421656
if (isset($values['_id'])) {
16431657
$values['id'] = $values['_id'];
1658+
unset($values['_id']);
1659+
}
1660+
1661+
foreach ($values as $key => $value) {
1662+
if (is_array($value)) {
1663+
$values[$key] = $this->aliasIdForResult($value);
1664+
}
16441665
}
16451666

16461667
return $values;

‎tests/QueryBuilderTest.php

Copy file name to clipboardExpand all lines: tests/QueryBuilderTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ public function testIdAlias($insertId, $queryId): void
10581058
DB::collection('items')->insert([$insertId => 'abc', 'name' => 'Karting']);
10591059
$item = DB::collection('items')->where($queryId, '=', 'abc')->first();
10601060
$this->assertNotNull($item);
1061-
$this->assertSame('abc', $item['_id']);
1061+
$this->assertSame('abc', $item['id']);
10621062
$this->assertSame('Karting', $item['name']);
10631063

10641064
DB::collection('items')->where($insertId, '=', 'abc')->update(['name' => 'Bike']);

‎tests/Ticket/GH2489Test.php

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Ticket;
6+
7+
use MongoDB\Laravel\Tests\Models\Location;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
/** @see https://github.com/mongodb/laravel-mongodb/issues/2783 */
11+
class GH2489Test extends TestCase
12+
{
13+
public function tearDown(): void
14+
{
15+
Location::truncate();
16+
}
17+
18+
public function testQuerySubdocumentsUsingWhereInId()
19+
{
20+
Location::insert([
21+
[
22+
'name' => 'Location 1',
23+
'images' => [
24+
['_id' => 1, 'uri' => 'image1.jpg'],
25+
['_id' => 2, 'uri' => 'image2.jpg'],
26+
],
27+
],
28+
[
29+
'name' => 'Location 2',
30+
'images' => [
31+
['_id' => 3, 'uri' => 'image3.jpg'],
32+
['_id' => 4, 'uri' => 'image4.jpg'],
33+
],
34+
],
35+
]);
36+
37+
// With _id
38+
$results = Location::whereIn('images._id', [1])->get();
39+
40+
$this->assertCount(1, $results);
41+
$this->assertSame('Location 1', $results->first()->name);
42+
43+
// With id
44+
$results = Location::whereIn('images.id', [1])->get();
45+
46+
$this->assertCount(1, $results);
47+
$this->assertSame('Location 1', $results->first()->name);
48+
}
49+
}

0 commit comments

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