forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.txt
79 lines (54 loc) · 2.4 KB
/
transactions.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
.. _laravel-transactions:
============
Transactions
============
.. facet::
:name: genre
:values: tutorial
.. meta::
:keywords: php framework, odm, code example
MongoDB transactions require the following software and topology:
- MongoDB version 4.0 or later
- A replica set deployment or sharded cluster
You can find more information :manual:`in the MongoDB docs </core/transactions/>`
.. code-block:: php
DB::transaction(function () {
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => '[email protected]']);
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
DB::collection('users')->where('name', 'john')->delete();
});
.. code-block:: php
// begin a transaction
DB::beginTransaction();
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => '[email protected]']);
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
DB::collection('users')->where('name', 'john')->delete();
// commit changes
DB::commit();
To abort a transaction, call the ``rollBack`` method at any point during the transaction:
.. code-block:: php
DB::beginTransaction();
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => '[email protected]']);
// Abort the transaction, discarding any data created as part of it
DB::rollBack();
.. note::
Transactions in MongoDB cannot be nested. DB::beginTransaction() function
will start new transactions in a new created or existing session and will
raise the RuntimeException when transactions already exist. See more in
MongoDB official docs :manual:`Transactions and Sessions </core/transactions/#transactions-and-sessions>`.
.. code-block:: php
DB::beginTransaction();
User::create(['name' => 'john', 'age' => 20, 'title' => 'admin']);
// This call to start a nested transaction will raise a RuntimeException
DB::beginTransaction();
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
DB::commit();
DB::rollBack();
Database Testing
----------------
For testing, the traits ``Illuminate\Foundation\Testing\DatabaseTransactions``
and ``Illuminate\Foundation\Testing\RefreshDatabase`` are not yet supported.
Instead, create migrations and use the ``DatabaseMigrations`` trait to reset
the database after each test:
.. code-block:: php
use Illuminate\Foundation\Testing\DatabaseMigrations;