From e51bc61e21441edb8d9fe5f1fb2df980138e1c0d Mon Sep 17 00:00:00 2001 From: Fuyuki Date: Fri, 4 Oct 2024 16:17:11 +0800 Subject: [PATCH 1/3] bugfix pluck Fix bug in pluck function. Cannot use object of type stdClass as array --- src/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 372dcf633..e8607adbc 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -854,7 +854,7 @@ public function pluck($column, $key = null) // Convert ObjectID's to strings if (((string) $key) === '_id') { $results = $results->map(function ($item) { - $item['_id'] = (string) $item['_id']; + $item->_id = (string) $item->id; return $item; }); From ddbf3f5dff80aa172973eb7d7f24f9366d4c2af4 Mon Sep 17 00:00:00 2001 From: Fuyuki Date: Fri, 4 Oct 2024 19:05:19 +0800 Subject: [PATCH 2/3] add test and remove string convert --- src/Query/Builder.php | 2 +- tests/Query/BuilderTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index e8607adbc..cabd5c827 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -854,7 +854,7 @@ public function pluck($column, $key = null) // Convert ObjectID's to strings if (((string) $key) === '_id') { $results = $results->map(function ($item) { - $item->_id = (string) $item->id; + $item->_id = $item->id; return $item; }); diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index c1587dc73..629cc1ba8 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -1573,4 +1573,28 @@ private static function getBuilder(): Builder return new Builder($connection, null, $processor); } + + public function testPluckObjectId() + { + DB::table('users')->insert([ + ['_id' => $id = new \MongoDB\BSON\ObjectId(), 'name' => 'Jane Doe', 'age' => 20], + ]); + + $names = DB::table('users')->pluck('name', '_id')->toArray(); + $this->assertCount(1, $names); + $this->assertArrayHasKey((string) $id, $names); + $this->assertEquals([(string) $id => 'Jane Doe'], $names); + } + + public function testPluckObjectIdWithIdAlias() + { + DB::table('users')->insert([ + ['_id' => $id = new \MongoDB\BSON\ObjectId(), 'name' => 'Jane Doe', 'age' => 20], + ]); + + $names = DB::table('users')->pluck('name', 'id')->toArray(); + $this->assertCount(1, $names); + $this->assertArrayHasKey((string) $id, $names); + $this->assertEquals([(string) $id => 'Jane Doe'], $names); + } } From 0b08a38cf5a75ac2555a70c6bf4b4a84293ba16c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 4 Oct 2024 17:56:27 +0200 Subject: [PATCH 3/3] Convertion of ObjectId to string is done in Laravel https://github.com/laravel/framework/blob/646520ad682d98b5211c6e26092259cfbe130b5c/src/Illuminate/Collections/Arr.php#L562 --- src/Query/Builder.php | 9 --------- tests/Query/BuilderTest.php | 24 ------------------------ tests/QueryBuilderTest.php | 11 +++++++++++ 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index cabd5c827..eeb5ffe8d 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -851,15 +851,6 @@ public function pluck($column, $key = null) { $results = $this->get($key === null ? [$column] : [$column, $key]); - // Convert ObjectID's to strings - if (((string) $key) === '_id') { - $results = $results->map(function ($item) { - $item->_id = $item->id; - - return $item; - }); - } - $p = Arr::pluck($results, $column, $key); return new Collection($p); diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index 629cc1ba8..c1587dc73 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -1573,28 +1573,4 @@ private static function getBuilder(): Builder return new Builder($connection, null, $processor); } - - public function testPluckObjectId() - { - DB::table('users')->insert([ - ['_id' => $id = new \MongoDB\BSON\ObjectId(), 'name' => 'Jane Doe', 'age' => 20], - ]); - - $names = DB::table('users')->pluck('name', '_id')->toArray(); - $this->assertCount(1, $names); - $this->assertArrayHasKey((string) $id, $names); - $this->assertEquals([(string) $id => 'Jane Doe'], $names); - } - - public function testPluckObjectIdWithIdAlias() - { - DB::table('users')->insert([ - ['_id' => $id = new \MongoDB\BSON\ObjectId(), 'name' => 'Jane Doe', 'age' => 20], - ]); - - $names = DB::table('users')->pluck('name', 'id')->toArray(); - $this->assertCount(1, $names); - $this->assertArrayHasKey((string) $id, $names); - $this->assertEquals([(string) $id => 'Jane Doe'], $names); - } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 523ad3411..136b1cf72 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -525,6 +525,17 @@ public function testPluck() $this->assertEquals([25], $age); } + public function testPluckObjectId() + { + $id = new ObjectId(); + DB::table('users')->insert([ + ['id' => $id, 'name' => 'Jane Doe'], + ]); + + $names = DB::table('users')->pluck('name', 'id')->toArray(); + $this->assertEquals([(string) $id => 'Jane Doe'], $names); + } + public function testList() { DB::table('items')->insert([