From 8fbbcc44b6b43bb47a1d3f2261649a4c8b08e0a0 Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Wed, 27 Mar 2024 17:15:51 -0400 Subject: [PATCH 01/12] DOCSP-35948: Write operations --- docs/fundamentals.txt | 27 +++++++++++++++++++ .../read-operations.txt} | 1 + docs/index.txt | 5 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 docs/fundamentals.txt rename docs/{retrieve.txt => fundamentals/read-operations.txt} (99%) diff --git a/docs/fundamentals.txt b/docs/fundamentals.txt new file mode 100644 index 000000000..6e2fa2c7f --- /dev/null +++ b/docs/fundamentals.txt @@ -0,0 +1,27 @@ +.. _laravel_fundamentals: + +============ +Fundamentals +============ + + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: php framework, odm + +.. toctree:: + :titlesonly: + :maxdepth: 1 + + /read-operations + /write-operations + +Learn how to perform the following tasks by using the {+odm-long+} in the +following pages: + +- :ref:`Read Operations ` +- :ref:`Write Operations ` + diff --git a/docs/retrieve.txt b/docs/fundamentals/read-operations.txt similarity index 99% rename from docs/retrieve.txt rename to docs/fundamentals/read-operations.txt index b607d3d4f..c7a835c9d 100644 --- a/docs/retrieve.txt +++ b/docs/fundamentals/read-operations.txt @@ -1,4 +1,5 @@ .. _laravel-fundamentals-retrieve: +.. _laravel-fundamentals-read-ops: ============== Retrieve Data diff --git a/docs/index.txt b/docs/index.txt index febdb9371..c9c45d2dd 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -15,7 +15,7 @@ Laravel MongoDB /quick-start Release Notes - /retrieve + /fundamentals /eloquent-models /query-builder /user-authentication @@ -53,7 +53,8 @@ Fundamentals To learn how to perform the following tasks by using the {+odm-short+}, see the following content: -- :ref:`laravel-fundamentals-retrieve` +- :ref:`laravel-fundamentals-read-ops` +- :ref:`laravel-fundamentals-write-ops` - :ref:`laravel-eloquent-models` - :ref:`laravel-query-builder` - :ref:`laravel-user-authentication` From a5a75ca1474cd2e4e1725d0b5f379d13c7455259 Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Thu, 28 Mar 2024 17:38:57 -0400 Subject: [PATCH 02/12] add examples --- docs/fundamentals/write-operations.txt | 118 ++++++++++++++++++ .../fundamentals/write-operations/Concert.php | 12 ++ .../write-operations/WriteOperationsTest.php | 99 +++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 docs/fundamentals/write-operations.txt create mode 100644 docs/includes/fundamentals/write-operations/Concert.php create mode 100644 docs/includes/fundamentals/write-operations/WriteOperationsTest.php diff --git a/docs/fundamentals/write-operations.txt b/docs/fundamentals/write-operations.txt new file mode 100644 index 000000000..09cf3b982 --- /dev/null +++ b/docs/fundamentals/write-operations.txt @@ -0,0 +1,118 @@ +.. _laravel-fundamentals-write-ops: + +================ +Write Operations +================ + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: insert, insert one, update, update one, upsert, delete, delete many, code example, mass assignment, eloquent model + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +Overview +-------- + +In this guide, you can learn how to use {+odm-short+} to perform +**write operations** on your MongoDB collections. Write operations include +inserting, updating, and deleting data based on criteria that you specify. + +This guide shows you how to perform the following tasks: + +- Insert Documents +- Modify Documents +- Delete Documents + +.. _laravel-fundamentals-insert-documents: + +Insert Documents +---------------- + +In this section, you can learn how to insert documents into MongoDB collections +from your Laravel application by using the {+odm-long+}. + +When you insert the documents, make sure that the data does not violate any +unique indexes on the collection. When inserting the first document of a +collection or creating a new collection, MongoDB automatically creates a +unique index on the ``_id`` field. + +For more ionformation creating indexes on MongoDB collections by using the +Laravel schema builder, see the :ref:`laravel-eloquent-indexes` guide. + +This section uses the following example model class to demonstrate how to +use Eloquent models to perform insert operations: + +.. literalinclude:: /includes/fundamentals/write-operations/Concert.php + :language: php + :dedent: + :caption: Concert.php + +.. tip:: + + The ``$fillable`` attribute lets you use Laravel mass assignment for insert + operations. To learn more about mass assignment, see :ref:`laravel-model-mass-assignment`. + +To learn more about Eloquent models in {+odm-short+} see the :ref:`laravel-eloquent-models` +section. + +Insert a Document Examples +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The examples in this section show how to use the ``save()`` Eloquent method to +insert an instance of a ``Concert`` model as a MongoDB document. + +When the ``save()`` method succeeds, the instance on which you called the +method contains the model. If it fails, the instance is assigned a null value. + +The example code performs the following actions: + +- Creates a new instance of the ``Concert`` model +- Assigns string values to the ``performer`` and ``venue`` field +- Assigns a date to the ``performanceDate`` field by using the ``Carbon`` + package. +- Inserts the document by calling the ``save()`` method. + +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php + :language: php + :dedent: + :start-after: begin model insert one + :end-before: end model insert one + +If you enable mass assignment by defining either the ``$fillable`` or +``$guarded`` attributes, you can use the Eloquent model ``create()`` method +to perform the insert in a single call as shown in the following example: + +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php + :language: php + :dedent: + :start-after: begin model insert one mass assign + :end-before: end model insert one mass assign + +To learn more about the Carbon PHP API extension, see the +`Carbon `__ GitHub repository. + +Insert Multiple Documents Example +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The examples in this section show how to use the ``insert()`` Eloquent method +to insert multiple instances of a ``Concert`` model as MongoDB documents. + +When the ``insert()`` method succeeds, the method returns the value ``1``. +If it fails, the method throws an exception. + +The example code passes an array containing the data for multiple models +to the ``insert()`` method: + +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php + :language: php + :dedent: + :start-after: begin model insert many + :end-before: end model insert many + diff --git a/docs/includes/fundamentals/write-operations/Concert.php b/docs/includes/fundamentals/write-operations/Concert.php new file mode 100644 index 000000000..e7c2dfc89 --- /dev/null +++ b/docs/includes/fundamentals/write-operations/Concert.php @@ -0,0 +1,12 @@ + 'datetime' ]; +} diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php new file mode 100644 index 000000000..d97dae535 --- /dev/null +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -0,0 +1,99 @@ + + + require_once __DIR__ . '/Concert.php'; + + Concert::truncate(); + + // begin model insert one + $concert = new Concert(); + $concert->performer = 'Mitsuko Uchida'; + $concert->venue = 'Carnegie Hall'; + $concert->performanceDate = Carbon::create(2024, 4, 1, 20, 0, 0, 'EST'); + $concert->save(); + // end model insert one + + $this->assertNotNull($concert); + + $result = Concert::first(); + $this->assertInstanceOf(Concert::class, $result); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testModelInsertMassAssign(): void + { + // + + require_once __DIR__ . '/Concert.php'; + + Concert::truncate(); + + // begin model insert one mass assign + $insertResult = Concert::create([ + 'performer' => 'The Rolling Stones', + 'venue' => 'Soldier Field', + 'performanceDate' => Carbon::create(2024, 6, 30, 20, 0, 0, 'CDT') + ]); + // end model insert one mass assign + + $this->assertNotNull($insertResult); + + $result = Concert::first(); + $this->assertInstanceOf(Concert::class, $result); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testModelInsertMany(): void + { + // + + require_once __DIR__ . '/Concert.php'; + + Concert::truncate(); + + // begin model insert many + $data = [ + [ + 'performer' => 'Brad Mehldau', + 'venue' => 'Philharmonie de Paris', + 'performanceDate' => Carbon::create(2025, 2, 12, 20, 0, 0, 'CET') + ], + [ + 'performer' => 'Billy Joel', + 'venue' => 'Madison Square Garden', + 'performanceDate' => Carbon::create(2024, 7, 25, 19, 0, 0, 'EST') + ] + ]; + Concert::insert($data); + // end model insert many + $results = Concert::get(); + + $this->assertEquals(2, count($results)); + } +} From a678179840d4a3dbce878ec36f710d81ef3d5fce Mon Sep 17 00:00:00 2001 From: ccho-mongodb Date: Thu, 28 Mar 2024 21:40:55 +0000 Subject: [PATCH 03/12] apply phpcbf formatting --- .../fundamentals/write-operations/Concert.php | 4 ++-- .../write-operations/WriteOperationsTest.php | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/docs/includes/fundamentals/write-operations/Concert.php b/docs/includes/fundamentals/write-operations/Concert.php index e7c2dfc89..9eaa51139 100644 --- a/docs/includes/fundamentals/write-operations/Concert.php +++ b/docs/includes/fundamentals/write-operations/Concert.php @@ -7,6 +7,6 @@ class Concert extends Model { protected $connection = 'mongodb'; - protected $fillable = [ 'performer', 'venue', 'performanceDate' ]; - protected $casts = [ 'performanceDate' => 'datetime' ]; + protected $fillable = ['performer', 'venue', 'performanceDate']; + protected $casts = ['performanceDate' => 'datetime']; } diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php index d97dae535..75c4cf116 100644 --- a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -4,12 +4,11 @@ namespace App\Http\Controllers; -use Illuminate\Database\Query\Builder; -use Illuminate\Support\Facades\DB; -use MongoDB\Laravel\Collection; -use MongoDB\Laravel\Tests\TestCase; use App\Models\Concert; use Carbon\Carbon; +use MongoDB\Laravel\Tests\TestCase; + +use function count; class WriteOperationsTest extends TestCase { @@ -55,7 +54,7 @@ public function testModelInsertMassAssign(): void $insertResult = Concert::create([ 'performer' => 'The Rolling Stones', 'venue' => 'Soldier Field', - 'performanceDate' => Carbon::create(2024, 6, 30, 20, 0, 0, 'CDT') + 'performanceDate' => Carbon::create(2024, 6, 30, 20, 0, 0, 'CDT'), ]); // end model insert one mass assign @@ -82,13 +81,13 @@ public function testModelInsertMany(): void [ 'performer' => 'Brad Mehldau', 'venue' => 'Philharmonie de Paris', - 'performanceDate' => Carbon::create(2025, 2, 12, 20, 0, 0, 'CET') + 'performanceDate' => Carbon::create(2025, 2, 12, 20, 0, 0, 'CET'), ], [ 'performer' => 'Billy Joel', 'venue' => 'Madison Square Garden', - 'performanceDate' => Carbon::create(2024, 7, 25, 19, 0, 0, 'EST') - ] + 'performanceDate' => Carbon::create(2024, 7, 25, 19, 0, 0, 'EST'), + ], ]; Concert::insert($data); // end model insert many From eea96e58b3aeabb8625baca0fc9446a3c9bd9455 Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Thu, 28 Mar 2024 18:26:37 -0400 Subject: [PATCH 04/12] wip --- docs/fundamentals.txt | 5 ++--- docs/fundamentals/write-operations.txt | 19 ++++++++++++++++--- .../write-operations/WriteOperationsTest.php | 8 ++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/docs/fundamentals.txt b/docs/fundamentals.txt index 6e2fa2c7f..738f8ec7a 100644 --- a/docs/fundamentals.txt +++ b/docs/fundamentals.txt @@ -4,7 +4,6 @@ Fundamentals ============ - .. facet:: :name: genre :values: reference @@ -16,8 +15,8 @@ Fundamentals :titlesonly: :maxdepth: 1 - /read-operations - /write-operations + /fundamentals/read-operations + /fundamentals/write-operations Learn how to perform the following tasks by using the {+odm-long+} in the following pages: diff --git a/docs/fundamentals/write-operations.txt b/docs/fundamentals/write-operations.txt index 09cf3b982..43d16667d 100644 --- a/docs/fundamentals/write-operations.txt +++ b/docs/fundamentals/write-operations.txt @@ -59,6 +59,10 @@ use Eloquent models to perform insert operations: The ``$fillable`` attribute lets you use Laravel mass assignment for insert operations. To learn more about mass assignment, see :ref:`laravel-model-mass-assignment`. + The ``$casts`` attribute instructs Laravel to convert attributes to common + data types. To learn more, see `Attribute Casting `__ + in the Laravel documentation. + To learn more about Eloquent models in {+odm-short+} see the :ref:`laravel-eloquent-models` section. @@ -71,13 +75,13 @@ insert an instance of a ``Concert`` model as a MongoDB document. When the ``save()`` method succeeds, the instance on which you called the method contains the model. If it fails, the instance is assigned a null value. -The example code performs the following actions: +This example code performs the following actions: - Creates a new instance of the ``Concert`` model - Assigns string values to the ``performer`` and ``venue`` field - Assigns a date to the ``performanceDate`` field by using the ``Carbon`` - package. -- Inserts the document by calling the ``save()`` method. + package +- Inserts the document by calling the ``save()`` method .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php :language: php @@ -85,6 +89,15 @@ The example code performs the following actions: :start-after: begin model insert one :end-before: end model insert one +You can retrieve the inserted document id value by accessing the ``id`` member +of the model as shown in the following code example: + +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php + :language: php + :dedent: + :start-after: begin inserted id + :end-before: end inserted id + If you enable mass assignment by defining either the ``$fillable`` or ``$guarded`` attributes, you can use the Eloquent model ``create()`` method to perform the insert in a single call as shown in the following example: diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php index 75c4cf116..b2902cd56 100644 --- a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -32,6 +32,12 @@ public function testModelInsert(): void $concert->save(); // end model insert one + // begin inserted id + $insertedId = $concert->id; + // end inserted id + + + print_r($insertedId); $this->assertNotNull($concert); $result = Concert::first(); @@ -89,8 +95,10 @@ public function testModelInsertMany(): void 'performanceDate' => Carbon::create(2024, 7, 25, 19, 0, 0, 'EST'), ], ]; + Concert::insert($data); // end model insert many + $results = Concert::get(); $this->assertEquals(2, count($results)); From 88024a53f97b2db47a0720af78643ae927a8ed15 Mon Sep 17 00:00:00 2001 From: ccho-mongodb Date: Thu, 28 Mar 2024 23:04:52 +0000 Subject: [PATCH 05/12] apply phpcbf formatting --- .../fundamentals/write-operations/WriteOperationsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php index b2902cd56..f00bf3792 100644 --- a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -9,6 +9,7 @@ use MongoDB\Laravel\Tests\TestCase; use function count; +use function print_r; class WriteOperationsTest extends TestCase { @@ -36,7 +37,6 @@ public function testModelInsert(): void $insertedId = $concert->id; // end inserted id - print_r($insertedId); $this->assertNotNull($concert); From 24fdd13fc71aed3f6722673f73d0195b8553227a Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Fri, 29 Mar 2024 11:25:16 -0400 Subject: [PATCH 06/12] add date info --- docs/fundamentals/write-operations.txt | 18 +++++++++++++----- .../write-operations/WriteOperationsTest.php | 7 ++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/fundamentals/write-operations.txt b/docs/fundamentals/write-operations.txt index 43d16667d..acf00f8ac 100644 --- a/docs/fundamentals/write-operations.txt +++ b/docs/fundamentals/write-operations.txt @@ -26,7 +26,7 @@ inserting, updating, and deleting data based on criteria that you specify. This guide shows you how to perform the following tasks: -- Insert Documents +- :ref:`laravel-fundamentals-insert-documents` - Modify Documents - Delete Documents @@ -43,7 +43,7 @@ unique indexes on the collection. When inserting the first document of a collection or creating a new collection, MongoDB automatically creates a unique index on the ``_id`` field. -For more ionformation creating indexes on MongoDB collections by using the +For more information creating indexes on MongoDB collections by using the Laravel schema builder, see the :ref:`laravel-eloquent-indexes` guide. This section uses the following example model class to demonstrate how to @@ -114,14 +114,22 @@ To learn more about the Carbon PHP API extension, see the Insert Multiple Documents Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The examples in this section show how to use the ``insert()`` Eloquent method +The example in this section show how to use the ``insert()`` Eloquent method to insert multiple instances of a ``Concert`` model as MongoDB documents. +This bulk insert method reduces the number of calls that your application needs +to make to save the documents. When the ``insert()`` method succeeds, the method returns the value ``1``. If it fails, the method throws an exception. -The example code passes an array containing the data for multiple models -to the ``insert()`` method: +The example code saves multiple models in a single call by passing them as +an array to ``insert()`` method: + +.. note:: + + This example wraps the dates in the `MongoDB\BSON\UTCDateTime <{+phplib-api+}/class.mongodb-bson-utcdatetime.php>`__ + class to convert it to a type that MongoDB can serialize because Laravel + skips attribute casting on bulk insert operations. .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php :language: php diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php index f00bf3792..645522ce6 100644 --- a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -6,6 +6,7 @@ use App\Models\Concert; use Carbon\Carbon; +use MongoDB\BSON\UTCDateTime; use MongoDB\Laravel\Tests\TestCase; use function count; @@ -37,8 +38,8 @@ public function testModelInsert(): void $insertedId = $concert->id; // end inserted id - print_r($insertedId); $this->assertNotNull($concert); + $this->assertNotNull($insertedId); $result = Concert::first(); $this->assertInstanceOf(Concert::class, $result); @@ -87,12 +88,12 @@ public function testModelInsertMany(): void [ 'performer' => 'Brad Mehldau', 'venue' => 'Philharmonie de Paris', - 'performanceDate' => Carbon::create(2025, 2, 12, 20, 0, 0, 'CET'), + 'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')), ], [ 'performer' => 'Billy Joel', 'venue' => 'Madison Square Garden', - 'performanceDate' => Carbon::create(2024, 7, 25, 19, 0, 0, 'EST'), + 'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')), ], ]; From 77bdbec9ab18874150a43dbfed26824dc45a7830 Mon Sep 17 00:00:00 2001 From: ccho-mongodb Date: Fri, 29 Mar 2024 15:28:29 +0000 Subject: [PATCH 07/12] apply phpcbf formatting --- .../fundamentals/write-operations/WriteOperationsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php index 645522ce6..f9f5d62fe 100644 --- a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -10,7 +10,6 @@ use MongoDB\Laravel\Tests\TestCase; use function count; -use function print_r; class WriteOperationsTest extends TestCase { From 831f06e0b26fed55d690696164bf7d8a602327e9 Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Fri, 29 Mar 2024 11:33:31 -0400 Subject: [PATCH 08/12] update title of read operations --- docs/fundamentals/read-operations.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/fundamentals/read-operations.txt b/docs/fundamentals/read-operations.txt index c7a835c9d..ff6c7814f 100644 --- a/docs/fundamentals/read-operations.txt +++ b/docs/fundamentals/read-operations.txt @@ -1,9 +1,9 @@ .. _laravel-fundamentals-retrieve: .. _laravel-fundamentals-read-ops: -============== -Retrieve Data -============== +=============== +Read Operations +=============== .. facet:: :name: genre From d813f23779739daa018c8e5adece573bc41ab2dd Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Fri, 29 Mar 2024 11:41:55 -0400 Subject: [PATCH 09/12] grammar fixes --- docs/fundamentals/write-operations.txt | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/fundamentals/write-operations.txt b/docs/fundamentals/write-operations.txt index acf00f8ac..b049b5a34 100644 --- a/docs/fundamentals/write-operations.txt +++ b/docs/fundamentals/write-operations.txt @@ -22,7 +22,7 @@ Overview In this guide, you can learn how to use {+odm-short+} to perform **write operations** on your MongoDB collections. Write operations include -inserting, updating, and deleting data based on criteria that you specify. +inserting, updating, and deleting data based on specified criteria. This guide shows you how to perform the following tasks: @@ -38,12 +38,12 @@ Insert Documents In this section, you can learn how to insert documents into MongoDB collections from your Laravel application by using the {+odm-long+}. -When you insert the documents, make sure that the data does not violate any +When you insert the documents, ensure the data does not violate any unique indexes on the collection. When inserting the first document of a collection or creating a new collection, MongoDB automatically creates a unique index on the ``_id`` field. -For more information creating indexes on MongoDB collections by using the +For more information on creating indexes on MongoDB collections by using the Laravel schema builder, see the :ref:`laravel-eloquent-indexes` guide. This section uses the following example model class to demonstrate how to @@ -69,8 +69,8 @@ section. Insert a Document Examples ~~~~~~~~~~~~~~~~~~~~~~~~~~ -The examples in this section show how to use the ``save()`` Eloquent method to -insert an instance of a ``Concert`` model as a MongoDB document. +These examples show how to use the ``save()`` Eloquent method to insert an +instance of a ``Concert`` model as a MongoDB document. When the ``save()`` method succeeds, the instance on which you called the method contains the model. If it fails, the instance is assigned a null value. @@ -89,8 +89,8 @@ This example code performs the following actions: :start-after: begin model insert one :end-before: end model insert one -You can retrieve the inserted document id value by accessing the ``id`` member -of the model as shown in the following code example: +You can retrieve the inserted document's ``_id`` value by accessing the model's +``id`` member as shown in the following code example: .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php :language: php @@ -114,13 +114,13 @@ To learn more about the Carbon PHP API extension, see the Insert Multiple Documents Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The example in this section show how to use the ``insert()`` Eloquent method -to insert multiple instances of a ``Concert`` model as MongoDB documents. -This bulk insert method reduces the number of calls that your application needs -to make to save the documents. +This example shows how to use the ``insert()`` Eloquent method to insert +multiple instances of a ``Concert`` model as MongoDB documents. This bulk +insert method reduces the number of calls your application needs to make +to save the documents. -When the ``insert()`` method succeeds, the method returns the value ``1``. -If it fails, the method throws an exception. +When the ``insert()`` method succeeds, it returns the value ``1``. If it +fails, it throws an exception. The example code saves multiple models in a single call by passing them as an array to ``insert()`` method: @@ -128,7 +128,7 @@ an array to ``insert()`` method: .. note:: This example wraps the dates in the `MongoDB\BSON\UTCDateTime <{+phplib-api+}/class.mongodb-bson-utcdatetime.php>`__ - class to convert it to a type that MongoDB can serialize because Laravel + class to convert it to a type MongoDB can serialize because Laravel skips attribute casting on bulk insert operations. .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php From 9fcce6fafc398e0f1826c2abf73a0dbdf14fc1da Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Fri, 29 Mar 2024 16:41:37 -0400 Subject: [PATCH 10/12] PRR fixes --- docs/fundamentals.txt | 3 +-- docs/fundamentals/write-operations.txt | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/fundamentals.txt b/docs/fundamentals.txt index 738f8ec7a..041388350 100644 --- a/docs/fundamentals.txt +++ b/docs/fundamentals.txt @@ -18,8 +18,7 @@ Fundamentals /fundamentals/read-operations /fundamentals/write-operations -Learn how to perform the following tasks by using the {+odm-long+} in the -following pages: +Learn how to use the {+odm-long+} to perform the following tasks: - :ref:`Read Operations ` - :ref:`Write Operations ` diff --git a/docs/fundamentals/write-operations.txt b/docs/fundamentals/write-operations.txt index b049b5a34..3c2f8233a 100644 --- a/docs/fundamentals/write-operations.txt +++ b/docs/fundamentals/write-operations.txt @@ -44,7 +44,8 @@ collection or creating a new collection, MongoDB automatically creates a unique index on the ``_id`` field. For more information on creating indexes on MongoDB collections by using the -Laravel schema builder, see the :ref:`laravel-eloquent-indexes` guide. +Laravel schema builder, see the :ref:`laravel-eloquent-indexes` section +of the Schema Builder documentation. This section uses the following example model class to demonstrate how to use Eloquent models to perform insert operations: @@ -57,13 +58,14 @@ use Eloquent models to perform insert operations: .. tip:: The ``$fillable`` attribute lets you use Laravel mass assignment for insert - operations. To learn more about mass assignment, see :ref:`laravel-model-mass-assignment`. + operations. To learn more about mass assignment, see :ref:`laravel-model-mass-assignment` + in the Eloquent Model Class documentation. The ``$casts`` attribute instructs Laravel to convert attributes to common data types. To learn more, see `Attribute Casting `__ in the Laravel documentation. -To learn more about Eloquent models in {+odm-short+} see the :ref:`laravel-eloquent-models` +To learn more about Eloquent models in {+odm-short+}, see the :ref:`laravel-eloquent-models` section. Insert a Document Examples @@ -72,13 +74,14 @@ Insert a Document Examples These examples show how to use the ``save()`` Eloquent method to insert an instance of a ``Concert`` model as a MongoDB document. -When the ``save()`` method succeeds, the instance on which you called the -method contains the model. If it fails, the instance is assigned a null value. +When the ``save()`` method succeeds, you can access the model instance on +which you called the method. If the operation fails, the model instance is +assigned a null value. This example code performs the following actions: - Creates a new instance of the ``Concert`` model -- Assigns string values to the ``performer`` and ``venue`` field +- Assigns string values to the ``performer`` and ``venue`` fields - Assigns a date to the ``performanceDate`` field by using the ``Carbon`` package - Inserts the document by calling the ``save()`` method @@ -109,7 +112,7 @@ to perform the insert in a single call as shown in the following example: :end-before: end model insert one mass assign To learn more about the Carbon PHP API extension, see the -`Carbon `__ GitHub repository. +:github:`Carbon ` GitHub repository. Insert Multiple Documents Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -123,7 +126,7 @@ When the ``insert()`` method succeeds, it returns the value ``1``. If it fails, it throws an exception. The example code saves multiple models in a single call by passing them as -an array to ``insert()`` method: +an array to the ``insert()`` method: .. note:: From 3c04080cc41e2169ecce0615f580cd5d1ffe52bd Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Wed, 3 Apr 2024 12:58:25 -0400 Subject: [PATCH 11/12] remove comments from tests --- .../fundamentals/write-operations/WriteOperationsTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php index f9f5d62fe..365d86dd3 100644 --- a/docs/includes/fundamentals/write-operations/WriteOperationsTest.php +++ b/docs/includes/fundamentals/write-operations/WriteOperationsTest.php @@ -19,8 +19,6 @@ class WriteOperationsTest extends TestCase */ public function testModelInsert(): void { - // - require_once __DIR__ . '/Concert.php'; Concert::truncate(); @@ -50,8 +48,6 @@ public function testModelInsert(): void */ public function testModelInsertMassAssign(): void { - // - require_once __DIR__ . '/Concert.php'; Concert::truncate(); @@ -76,8 +72,6 @@ public function testModelInsertMassAssign(): void */ public function testModelInsertMany(): void { - // - require_once __DIR__ . '/Concert.php'; Concert::truncate(); From e8c1491836ad83d5e1ba784e61c4df1291962da6 Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Thu, 4 Apr 2024 13:55:16 -0400 Subject: [PATCH 12/12] PRR fixes --- docs/fundamentals/write-operations.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/write-operations.txt b/docs/fundamentals/write-operations.txt index 3c2f8233a..5afcc5010 100644 --- a/docs/fundamentals/write-operations.txt +++ b/docs/fundamentals/write-operations.txt @@ -76,7 +76,7 @@ instance of a ``Concert`` model as a MongoDB document. When the ``save()`` method succeeds, you can access the model instance on which you called the method. If the operation fails, the model instance is -assigned a null value. +assigned ``null``. This example code performs the following actions: