Skip to content

Commit eee519b

Browse files
committed
Fix id alias in nested array
1 parent 01ad545 commit eee519b

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/Query/Builder.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,11 +1038,6 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
10381038
{
10391039
$params = func_get_args();
10401040

1041-
// Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
1042-
if ($column === 'id') {
1043-
$params[0] = $column = '_id';
1044-
}
1045-
10461041
// Remove the leading $ from operators.
10471042
if (func_num_args() >= 3) {
10481043
$operator = &$params[1];
@@ -1090,16 +1085,21 @@ protected function compileWheres(): array
10901085
// Convert column name to string to use as array key
10911086
if (isset($where['column'])) {
10921087
$where['column'] = (string) $where['column'];
1093-
}
10941088

1095-
// Convert id's.
1096-
if (isset($where['column']) && ($where['column'] === '_id' || str_ends_with($where['column'], '._id'))) {
1097-
if (isset($where['values'])) {
1098-
// Multiple values.
1099-
$where['values'] = array_map($this->convertKey(...), $where['values']);
1100-
} elseif (isset($where['value'])) {
1101-
// Single value.
1102-
$where['value'] = $this->convertKey($where['value']);
1089+
// Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
1090+
if ($where['column'] === 'id') {
1091+
$where['column'] = '_id';
1092+
}
1093+
1094+
// Convert id's.
1095+
if ($where['column'] === '_id' || str_ends_with($where['column'], '._id')) {
1096+
if (isset($where['values'])) {
1097+
// Multiple values.
1098+
$where['values'] = array_map($this->convertKey(...), $where['values']);
1099+
} elseif (isset($where['value'])) {
1100+
// Single value.
1101+
$where['value'] = $this->convertKey($where['value']);
1102+
}
11031103
}
11041104
}
11051105

tests/Query/BuilderTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ public static function provideQueryBuilderToMql(): iterable
124124
];
125125

126126
// Nested array are not flattened like in the Eloquent builder. MongoDB can compare objects.
127+
// When id is used as data field name, it's not converted to _id
127128
$array = [['issue' => 45582], ['id' => 2], [3]];
128129
yield 'whereIn nested array' => [
129-
['find' => [['id' => ['$in' => $array]], []]],
130+
['find' => [['_id' => ['$in' => $array]], []]],
130131
fn (Builder $builder) => $builder->whereIn('id', $array),
131132
];
132133

@@ -170,7 +171,7 @@ public static function provideQueryBuilderToMql(): iterable
170171

171172
/** @see DatabaseQueryBuilderTest::testEmptyWhereIns */
172173
yield 'whereIn empty array' => [
173-
['find' => [['id' => ['$in' => []]], []]],
174+
['find' => [['_id' => ['$in' => []]], []]],
174175
fn (Builder $builder) => $builder->whereIn('id', []),
175176
];
176177

@@ -553,12 +554,12 @@ function (Builder $builder) {
553554

554555
/** @see DatabaseQueryBuilderTest::testWhereBetweens() */
555556
yield 'whereBetween array of numbers' => [
556-
['find' => [['id' => ['$gte' => 1, '$lte' => 2]], []]],
557+
['find' => [['_id' => ['$gte' => 1, '$lte' => 2]], []]],
557558
fn (Builder $builder) => $builder->whereBetween('id', [1, 2]),
558559
];
559560

560561
yield 'whereBetween nested array of numbers' => [
561-
['find' => [['id' => ['$gte' => [1], '$lte' => [2, 3]]], []]],
562+
['find' => [['_id' => ['$gte' => [1], '$lte' => [2, 3]]], []]],
562563
fn (Builder $builder) => $builder->whereBetween('id', [[1], [2, 3]]),
563564
];
564565

@@ -579,7 +580,7 @@ function (Builder $builder) {
579580
];
580581

581582
yield 'whereBetween collection' => [
582-
['find' => [['id' => ['$gte' => 1, '$lte' => 2]], []]],
583+
['find' => [['_id' => ['$gte' => 1, '$lte' => 2]], []]],
583584
fn (Builder $builder) => $builder->whereBetween('id', collect([1, 2])),
584585
];
585586

0 commit comments

Comments
 (0)