Skip to content

Commit efab63b

Browse files
authored
PHPORM-227 Fix single document upsert (#3100)
1 parent 80694e4 commit efab63b

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [4.7.2] - coming soon
5+
6+
* Add `Query\Builder::upsert()` method with a single document by @GromNaN in [#3100](https://github.com/mongodb/laravel-mongodb/pull/3100)
7+
48
## [4.7.1] - 2024-07-25
59

610
* Fix registration of `BusServiceProvider` for compatibility with Horizon by @GromNaN in [#3071](https://github.com/mongodb/laravel-mongodb/pull/3071)

src/Query/Builder.php

+5
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,11 @@ public function upsert(array $values, $uniqueBy, $update = null): int
732732
return 0;
733733
}
734734

735+
// Single document provided
736+
if (! array_is_list($values)) {
737+
$values = [$values];
738+
}
739+
735740
$this->applyBeforeQueryCallbacks();
736741

737742
$options = $this->inheritConnectionOptions();

tests/ModelTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ public function testUpsert()
168168
$this->assertSame('bar2', User::where('email', 'foo')->first()->name);
169169

170170
// If no update fields are specified, all fields are updated
171-
$result = User::upsert([
172-
['email' => 'foo', 'name' => 'bar3'],
173-
], 'email');
171+
// Test single document update
172+
$result = User::upsert(['email' => 'foo', 'name' => 'bar3'], 'email');
174173

175174
$this->assertSame(1, $result);
176175
$this->assertSame(2, User::count());

tests/QueryBuilderTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,8 @@ public function testUpsert()
632632
$this->assertSame('bar2', DB::collection('users')->where('email', 'foo')->first()['name']);
633633

634634
// If no update fields are specified, all fields are updated
635-
$result = DB::collection('users')->upsert([
636-
['email' => 'foo', 'name' => 'bar3'],
637-
], 'email');
635+
// Test single document update
636+
$result = DB::collection('users')->upsert(['email' => 'foo', 'name' => 'bar3'], 'email');
638637

639638
$this->assertSame(1, $result);
640639
$this->assertSame(2, DB::collection('users')->count());

0 commit comments

Comments
 (0)