File tree Expand file tree Collapse file tree
user_guide_src/source/database Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -97,3 +97,20 @@ If you would like to run transactions manually you can do so as follows:
9797
9898.. note :: Make sure to use ``$this->db->transBegin()`` when running manual
9999 transactions, **NOT ** ``$this->db->transStart() ``.
100+
101+ Nested Transactions
102+ ===================
103+
104+ In CodeIgniter, transactions can be nested in a way such that only the
105+ outmost or top-level transaction commands are executed. You can include as
106+ many pairs of ``transStart() ``/``transComplete() `` or ``transBegin() ``/``transCommit() ``/``transRollback() ``
107+ as you want inside a transaction block and so on. CodeIgniter will keep
108+ track of the transaction "depth" and only take action at the outermost layer
109+ (zero depth).
110+
111+ .. literalinclude :: transactions/007.php
112+
113+ .. note :: In case the structure is far more complex, it's your responsibility
114+ to ensure that the inner transactions can reach the outermost layer again
115+ in order to be fully executed by the database, thus prevents unintended
116+ commits/rollbacks.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ $ this ->db ->transStart (); // actually starts a transaction
4+ $ this ->db ->query ('SOME QUERY 1 ... ' );
5+ $ this ->db ->transStart (); // doesn't necessarily start another transaction
6+ $ this ->db ->query ('SOME QUERY 2 ... ' );
7+ $ this ->db ->transComplete (); // doesn't necessarily end the transaction, but required to finish the inner transaction
8+ $ this ->db ->query ('SOME QUERY 3 ... ' );
9+ $ this ->db ->transComplete (); // actually ends the transaction
You can’t perform that action at this time.
0 commit comments