Skip to content

PHPORM-320 Fix aliasing .id field path to ._id #3353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ jobs:
- "11.*"
- "12.*"
driver:
- 1
- 2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to change the default driver version that is tested to match the ruleset from #3347

include:
- php: "8.1"
laravel: "10.*"
mongodb: "5.0"
mode: "low-deps"
os: "ubuntu-latest"
driver: 1.x
driver_version: "1.21.0"
driver: 1
- php: "8.3"
laravel: "11.*"
mongodb: "8.0"
os: "ubuntu-latest"
driver: 1
- php: "8.4"
laravel: "12.*"
mongodb: "8.0"
os: "ubuntu-latest"
driver: 2
driver: 1
exclude:
- php: "8.1"
laravel: "11.*"
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@ private function aliasIdForQuery(array $values, bool $root = true): array
}

// ".id" subfield are alias for "._id"
if (str_ends_with($key, '.id') && ($root || $this->connection->getRenameEmbeddedIdField())) {
if (str_ends_with($key, '.id') && $this->connection->getRenameEmbeddedIdField()) {
$newkey = substr($key, 0, -3) . '._id';
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) {
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey));
Expand Down
89 changes: 73 additions & 16 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1599,29 +1599,86 @@ public static function getEloquentMethodsNotSupported()
yield 'orWhereIntegerNotInRaw' => [fn (Builder $builder) => $builder->orWhereIntegerNotInRaw('id', ['1a', 2])];
}

public function testRenameEmbeddedIdFieldCanBeDisabled()
#[DataProvider('provideDisableRenameEmbeddedIdField')]
public function testDisableRenameEmbeddedIdField(array $expected, Closure $build)
{
$builder = $this->getBuilder(false);
$this->assertFalse($builder->getConnection()->getRenameEmbeddedIdField());

$mql = $builder
->where('id', '=', 10)
->where('nested.id', '=', 20)
->where('embed', '=', ['id' => 30])
->toMql();

$this->assertEquals([
'find' => [
[
'$and' => [
['_id' => 10],
['nested.id' => 20],
['embed' => ['id' => 30]],
$mql = $build($builder)->toMql();

$this->assertEquals($expected, $mql);
}

public static function provideDisableRenameEmbeddedIdField()
{
yield 'rename embedded id field' => [
[
'find' => [
[
'$and' => [
['_id' => 10],
['nested.id' => 20],
['embed' => ['id' => 30]],
],
],
['typeMap' => ['root' => 'object', 'document' => 'array']],
],
],
fn (Builder $builder) => $builder->where('id', '=', 10)
->where('nested.id', '=', 20)
->where('embed', '=', ['id' => 30]),
];

yield 'rename root id' => [
['find' => [['_id' => 10], ['typeMap' => ['root' => 'object', 'document' => 'array']]]],
fn (Builder $builder) => $builder->where('id', '=', 10),
];

yield 'nested id not renamed' => [
['find' => [['nested.id' => 20], ['typeMap' => ['root' => 'object', 'document' => 'array']]]],
fn (Builder $builder) => $builder->where('nested.id', '=', 20),
];

yield 'embed id not renamed' => [
['find' => [['embed' => ['id' => 30]], ['typeMap' => ['root' => 'object', 'document' => 'array']]]],
fn (Builder $builder) => $builder->where('embed', '=', ['id' => 30]),
];

yield 'nested $and in $or' => [
[
'find' => [
[
'$or' => [
[
'$and' => [
['_id' => 10],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are flaws in the conversion if you use advanced queries. This transformation from id to _id is done in the compileWheres from the “column name”:

// Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
if ($where['column'] === 'id') {
$where['column'] = '_id';
}

['nested.id' => 20],
['embed' => ['id' => 30]],
],
],
[
'$and' => [
['_id' => 40],
['nested.id' => 50],
['embed' => ['id' => 60]],
],
],
],
],
['typeMap' => ['root' => 'object', 'document' => 'array']],
],
['typeMap' => ['root' => 'object', 'document' => 'array']],
],
], $mql);
fn (Builder $builder) => $builder->orWhere(function (Builder $builder) {
return $builder->where('id', '=', 10)
->where('nested.id', '=', 20)
->where('embed', '=', ['id' => 30]);
})->orWhere(function (Builder $builder) {
return $builder->where('id', '=', 40)
->where('nested.id', '=', 50)
->where('embed', '=', ['id' => 60]);
}),
];
}

private function getBuilder(bool $renameEmbeddedIdField = true): Builder
Expand Down
Loading