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 47c41e6

Browse files
committed
PHPORM-236 Remove _id from query results
1 parent 7551f76 commit 47c41e6

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [5.0.0] - next
55

66
* Remove support for Laravel 10 by @GromNaN in [#3123](https://github.com/mongodb/laravel-mongodb/pull/3123)
7-
* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040)
7+
* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040) and [#3136](https://github.com/mongodb/laravel-mongodb/pull/3136)
88
* **BREAKING CHANGE** Make Query\Builder return objects instead of array to match Laravel behavior by @GromNaN in [#3107](https://github.com/mongodb/laravel-mongodb/pull/3107)
99
* **BREAKING CHANGE** In DB query results, convert BSON `UTCDateTime` objects into `Carbon` date with the default timezone by @GromNaN in [#3119](https://github.com/mongodb/laravel-mongodb/pull/3119)
1010
* Remove `MongoFailedJobProvider`, replaced by Laravel `DatabaseFailedJobProvider` by @GromNaN in [#3122](https://github.com/mongodb/laravel-mongodb/pull/3122)

‎src/Query/Builder.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
16161616
private function aliasIdForQuery(array $values): array
16171617
{
16181618
if (array_key_exists('id', $values)) {
1619-
if (array_key_exists('_id', $values)) {
1619+
if (array_key_exists('_id', $values) && $values['id'] !== $values['_id']) {
16201620
throw new InvalidArgumentException('Cannot have both "id" and "_id" fields.');
16211621
}
16221622

@@ -1627,7 +1627,7 @@ private function aliasIdForQuery(array $values): array
16271627
foreach ($values as $key => $value) {
16281628
if (is_string($key) && str_ends_with($key, '.id')) {
16291629
$newkey = substr($key, 0, -3) . '._id';
1630-
if (array_key_exists($newkey, $values)) {
1630+
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) {
16311631
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey));
16321632
}
16331633

@@ -1659,7 +1659,7 @@ private function aliasIdForResult(array|object $values): array|object
16591659
if (is_array($values)) {
16601660
if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) {
16611661
$values['id'] = $values['_id'];
1662-
//unset($values['_id']);
1662+
unset($values['_id']);
16631663
}
16641664

16651665
foreach ($values as $key => $value) {
@@ -1675,7 +1675,7 @@ private function aliasIdForResult(array|object $values): array|object
16751675
if ($values instanceof stdClass) {
16761676
if (property_exists($values, '_id') && ! property_exists($values, 'id')) {
16771677
$values->id = $values->_id;
1678-
//unset($values->_id);
1678+
unset($values->_id);
16791679
}
16801680

16811681
foreach (get_object_vars($values) as $key => $value) {

‎tests/QueryBuilderTest.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,25 +449,34 @@ public function testDistinct()
449449

450450
public function testCustomId()
451451
{
452+
$tags = [['id' => 'sharp', 'name' => 'Sharp']];
452453
DB::table('items')->insert([
453-
['id' => 'knife', 'type' => 'sharp', 'amount' => 34],
454-
['id' => 'fork', 'type' => 'sharp', 'amount' => 20],
454+
['id' => 'knife', 'type' => 'sharp', 'amount' => 34, 'tags' => $tags],
455+
['id' => 'fork', 'type' => 'sharp', 'amount' => 20, 'tags' => $tags],
455456
['id' => 'spoon', 'type' => 'round', 'amount' => 3],
456457
]);
457458

458459
$item = DB::table('items')->find('knife');
459460
$this->assertEquals('knife', $item->id);
461+
$this->assertObjectNotHasProperty('_id', $item);
462+
$this->assertEquals('sharp', $item->tags[0]['id']);
463+
$this->assertArrayNotHasKey('_id', $item->tags[0]);
460464

461465
$item = DB::table('items')->where('id', 'fork')->first();
462466
$this->assertEquals('fork', $item->id);
463467

468+
// tags.id is translated into tags._id in query
469+
$items = DB::table('items')->whereIn('tags.id', ['sharp'])->get();
470+
$this->assertCount(2, $items);
471+
464472
DB::table('users')->insert([
465473
['id' => 1, 'name' => 'Jane Doe'],
466474
['id' => 2, 'name' => 'John Doe'],
467475
]);
468476

469477
$item = DB::table('users')->find(1);
470478
$this->assertEquals(1, $item->id);
479+
$this->assertObjectNotHasProperty('_id', $item);
471480
}
472481

473482
public function testTake()

‎tests/Queue/Failed/DatabaseFailedJobProviderTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testAll(): void
7777
$all = $this->getProvider()->all();
7878

7979
$this->assertCount(5, $all);
80-
$this->assertEquals(new ObjectId(sprintf('%024d', 5)), $all[0]->_id);
80+
$this->assertEquals(new ObjectId(sprintf('%024d', 5)), $all[0]->id);
8181
$this->assertEquals(sprintf('%024d', 5), $all[0]->id, 'id field is added for compatibility with DatabaseFailedJobProvider');
8282
}
8383

@@ -89,7 +89,7 @@ public function testFindAndForget(): void
8989
$found = $provider->find($id);
9090

9191
$this->assertIsObject($found, 'The job is found');
92-
$this->assertEquals(new ObjectId($id), $found->_id);
92+
$this->assertEquals(new ObjectId($id), $found->id);
9393
$this->assertObjectHasProperty('failed_at', $found);
9494

9595
// Delete the job

0 commit comments

Comments
(0)

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