@@ -258,6 +258,86 @@ An **upsert** operation lets you perform an update or insert in a single
258
258
operation. This operation streamlines the task of updating a document or
259
259
inserting one if it does not exist.
260
260
261
+ Starting in v4.7, you can perform an upsert operation by using either of
262
+ the following methods:
263
+
264
+ - Use the ``upsert()`` method. When you use this method,
265
+ you can perform a **batch upsert** to change or insert multiple documents
266
+ in one operation.
267
+
268
+ - Use the ``update()`` method and specify the ``upsert``
269
+ option to update all documents that match the query filter or insert
270
+ one document if no documents are matched. Only this method is
271
+ supported in versions v4.6 and earlier.
272
+
273
+ Upsert Method
274
+ ~~~~~~~~~~~~~
275
+
276
+ The ``upsert()`` query builder method accepts the following parameters:
277
+
278
+ - Array of fields and values that specify documents to update or insert.
279
+ - Array of fields that uniquely identify documents in your first array parameter.
280
+ - Array of fields to update if a matching document exists. If you omit
281
+ this parameter, {+odm-short+} updates all fields.
282
+
283
+ To specify an upsert in the ``upsert()`` method, set parameters
284
+ as shown in the following code example:
285
+
286
+ .. code-block:: php
287
+ :emphasize-lines: 4
288
+ :copyable: false
289
+
290
+ YourModel::upsert(
291
+ [/* documents to update or insert */],
292
+ '/* unique field */', [/* fields to update */']
293
+ );
294
+
295
+ Example
296
+ ^^^^^^^
297
+
298
+ This example shows how to use the ``upsert()``
299
+ method to perform an update or insert in a single operation. Click the
300
+ :guilabel:`{+code-output-label+}` button to see the resulting data changes if
301
+ there is an ``'Angel Olson'`` document in the collection already:
302
+
303
+ .. io-code-block::
304
+
305
+ .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php
306
+ :language: php
307
+ :dedent:
308
+ :start-after: begin model upsert
309
+ :end-before: end model upsert
310
+
311
+ .. output::
312
+ :language: json
313
+ :visible: false
314
+
315
+ {
316
+ "_id": "...",
317
+ "performer": "Angel Olsen",
318
+ "venue": "State Theatre",
319
+ "genres": [
320
+ "indie",
321
+ "rock"
322
+ ],
323
+ "ticketsSold": 275,
324
+ "updated_at": ...
325
+ },
326
+ {
327
+ "_id": "...",
328
+ "performer": "Darondo",
329
+ "venue": "Cafe du Nord",
330
+ "ticketsSold": 300,
331
+ "updated_at": ...
332
+ }
333
+
334
+ In the ``'Angel Olsen'`` document, the ``venue`` field value is not
335
+ changed, as the upsert specifies that the update only applies to the
336
+ ``ticketsSold`` field.
337
+
338
+ Update Method
339
+ ~~~~~~~~~~~~~
340
+
261
341
To specify an upsert in an ``update()`` method, set the ``upsert`` option to
262
342
``true`` as shown in the following code example:
263
343
@@ -278,8 +358,8 @@ following actions:
278
358
- If the query matches zero documents, the ``update()`` method inserts a
279
359
document that contains the update data and the equality match criteria data.
280
360
281
- Upsert Example
282
- ~~~~~~~~~~~~~~
361
+ Example
362
+ ^^^^^^^
283
363
284
364
This example shows how to pass the ``upsert`` option to the ``update()``
285
365
method to perform an update or insert in a single operation. Click the
@@ -291,8 +371,8 @@ matching documents exist:
291
371
.. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php
292
372
:language: php
293
373
:dedent:
294
- :start-after: begin model upsert
295
- :end-before: end model upsert
374
+ :start-after: begin model update upsert
375
+ :end-before: end model update upsert
296
376
297
377
.. output::
298
378
:language: json
0 commit comments