Skip to content

Commit b2e90d7

Browse files
committed
docs: add sample code when DBDebug is true
1 parent 5d6a907 commit b2e90d7

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

user_guide_src/source/database/transactions.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,27 @@ contrast, we've implemented a smart transaction system that does all
3232
this for you automatically (you can also manage your transactions
3333
manually if you choose to, but there's really no benefit).
3434

35+
.. note:: Since v4.3.0, ``DBDebug`` is true by default in all environments.
36+
When ``DBDebug`` is true, if a query error occurs, all the queries
37+
will be rolled backed, and a ``DatabaseException`` will be thrown.
38+
In previous versions, ``DBDebug`` was false only in production environment,
39+
and different database drivers might throw different exception classes.
40+
3541
Running Transactions
3642
====================
3743

3844
To run your queries using transactions you will use the
3945
``$this->db->transStart()`` and ``$this->db->transComplete()`` methods as
4046
follows:
4147

48+
.. literalinclude:: transactions/008.php
49+
4250
.. literalinclude:: transactions/001.php
4351

4452
You can run as many queries as you want between the ``transStart()``/``transComplete()``
4553
methods and they will all be committed or rolled back based on the success
4654
or failure of any given query.
4755

48-
.. note:: Since v4.3.0, ``DBDebug`` is true by default in all environments.
49-
When ``DBDebug`` is true, if a query error occurs, all the queries
50-
will be rolled backed, and an exception will be thrown.
51-
In previous versions, ``DBDebug`` was false only in production environment,
52-
and different database drivers might throw different exception classes.
53-
5456
Strict Mode
5557
===========
5658

user_guide_src/source/database/transactions/001.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
// When DBDebug in the Database Config is false.
4+
35
$this->db->transStart();
46
$this->db->query('AN SQL QUERY...');
57
$this->db->query('ANOTHER QUERY...');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
// When DBDebug in the Database Config is true.
4+
5+
use CodeIgniter\Database\Exceptions\DatabaseException;
6+
7+
try {
8+
$this->db->transStart();
9+
$this->db->query('AN SQL QUERY...');
10+
$this->db->query('ANOTHER QUERY...');
11+
$this->db->query('AND YET ANOTHER QUERY...');
12+
$this->db->transComplete();
13+
} catch (DatabaseException $e) {
14+
// Automatically rolled back already.
15+
}

0 commit comments

Comments
 (0)