Skip to content

Commit 6cf36d3

Browse files
committed
Recursively replace .id with ._id
1 parent 6f0117d commit 6cf36d3

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/Query/Builder.php

Lines 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/Ticket/GH2489Test.php

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

0 commit comments

Comments
 (0)