File tree Expand file tree Collapse file tree 4 files changed +27
-2
lines changed Expand file tree Collapse file tree 4 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
4
4
## [ 4.4.0] - unreleased
5
5
6
6
* Support collection name prefix by @GromNaN in [ #2930 ] ( https://github.com/mongodb/laravel-mongodb/pull/2930 )
7
+ * Ignore ` _id: null ` to let MongoDB generate an ` ObjectId ` by @GromNaN in [ #2969 ] ( https://github.com/mongodb/laravel-mongodb/pull/2969 )
7
8
* Add ` mongodb ` driver for Batching by @GromNaN in [ #2904 ] ( https://github.com/mongodb/laravel-mongodb/pull/2904 )
8
9
* Rename queue option ` table ` to ` collection `
9
10
* Replace queue option ` expire ` with ` retry_after `
Original file line number Diff line number Diff line change 21
21
use function collect ;
22
22
use function is_array ;
23
23
use function iterator_to_array ;
24
+ use function json_encode ;
24
25
25
26
/** @method \MongoDB\Laravel\Query\Builder toBase() */
26
27
class Builder extends EloquentBuilder
@@ -210,8 +211,8 @@ public function raw($value = null)
210
211
*/
211
212
public function createOrFirst (array $ attributes = [], array $ values = []): Model
212
213
{
213
- if ($ attributes === []) {
214
- throw new InvalidArgumentException ('You must provide attributes to check for duplicates ' );
214
+ if ($ attributes === [] || $ attributes === [ ' _id ' => null ] ) {
215
+ throw new InvalidArgumentException ('You must provide attributes to check for duplicates. Got ' . json_encode ( $ attributes ) );
215
216
}
216
217
217
218
// Apply casting and default values to the attributes
Original file line number Diff line number Diff line change @@ -745,6 +745,12 @@ protected function isBSON(mixed $value): bool
745
745
*/
746
746
public function save (array $ options = [])
747
747
{
748
+ // SQL databases would use autoincrement the id field if set to null.
749
+ // Apply the same behavior to MongoDB with _id only, otherwise null would be stored.
750
+ if (array_key_exists ('_id ' , $ this ->attributes ) && $ this ->attributes ['_id ' ] === null ) {
751
+ unset($ this ->attributes ['_id ' ]);
752
+ }
753
+
748
754
$ saved = parent ::save ($ options );
749
755
750
756
// Clear list of unset fields
Original file line number Diff line number Diff line change @@ -1151,4 +1151,21 @@ public function testUpdateOrCreate(array $criteria)
1151
1151
$ this ->assertEquals ($ createdAt , $ checkUser ->created_at ->getTimestamp ());
1152
1152
$ this ->assertEquals ($ updatedAt , $ checkUser ->updated_at ->getTimestamp ());
1153
1153
}
1154
+
1155
+ public function testCreateWithNullId ()
1156
+ {
1157
+ $ user = User::create (['_id ' => null , 'email ' => 'foo@bar ' ]);
1158
+ $ this ->assertNotNull (ObjectId::class, $ user ->id );
1159
+ $ this ->assertSame (1 , User::count ());
1160
+ }
1161
+
1162
+ public function testUpdateOrCreateWithNullId ()
1163
+ {
1164
+ $ this ->expectException (InvalidArgumentException::class);
1165
+ $ this ->expectExceptionMessage ('You must provide attributes to check for duplicates ' );
1166
+ User::updateOrCreate (
1167
+ ['_id ' => null ],
1168
+ ['email ' => 'jane.doe@example.com ' ],
1169
+ );
1170
+ }
1154
1171
}
You can’t perform that action at this time.
0 commit comments