Skip to content

Commit add453f

Browse files
committed
add docs
1 parent 8300587 commit add453f

File tree

6 files changed

+97
-4
lines changed

6 files changed

+97
-4
lines changed

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ Others
123123
------
124124

125125
- Added new ``timezone`` option to connection array in ``Config\Database`` config. This ensures consistent timestamps between model operations and database functions like ``NOW()``. Supported drivers: **MySQLi**, **Postgre**, and **OCI8**. See :ref:`database-config-timezone` for details.
126+
- Added :php:class:`UniqueConstraintViolationException <CodeIgniter\\Database\\Exceptions\\UniqueConstraintViolationException>` which extends ``DatabaseException`` and is thrown on duplicate key (unique constraint) violations across all database drivers. See :ref:`database-unique-constraint-violation`.
127+
- Added ``$db->getLastException()`` which returns the typed exception even when ``DBDebug`` is ``false``. See :ref:`database-get-last-exception`.
128+
- Added ``DatabaseException::getDatabaseCode()`` returning the native driver error code as ``int|string``; ``getCode()`` is constrained to ``int`` by PHP's ``Throwable`` interface and cannot carry string SQLSTATE codes.
126129

127130
Model
128131
=====

user_guide_src/source/database/queries.rst

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,63 @@ placeholders in the query:
203203
Handling Errors
204204
***************
205205

206+
.. note:: It is strongly recommended to keep ``DBDebug`` set to ``true``
207+
(the default). This ensures all query failures surface immediately as
208+
exceptions, preventing silent data corruption. Setting it to ``false``
209+
is considered legacy behaviour and may be deprecated in a future version.
210+
211+
DBDebug Enabled (Recommended)
212+
=============================
213+
214+
When :ref:`DBDebug <database-config-explanation-of-values>` is ``true``
215+
(the default), any query failure throws a ``DatabaseException`` or one of
216+
its subclasses, which you can catch and handle:
217+
218+
.. literalinclude:: queries/030.php
219+
220+
.. _database-unique-constraint-violation:
221+
222+
UniqueConstraintViolationException
223+
----------------------------------
224+
225+
.. versionadded:: 4.8.0
226+
227+
``UniqueConstraintViolationException`` extends ``DatabaseException`` and is
228+
thrown specifically when a query fails due to a duplicate key or unique
229+
constraint violation. Catching it separately allows you to handle this case
230+
without inspecting raw driver-specific error codes.
231+
232+
DBDebug Disabled
233+
================
234+
235+
When ``DBDebug`` is ``false``, query failures return ``false`` instead of
236+
throwing. Two methods are available to inspect what went wrong.
237+
206238
$db->error()
207-
============
239+
------------
208240

209-
If you need to get the last error that has occurred, the ``error()`` method
210-
will return an array containing its code and message. Here's a quick
211-
example:
241+
The ``error()`` method returns an array with ``code`` and ``message`` keys
242+
describing the last error. Error codes are driver-specific (an **int** for
243+
MySQLi, SQLite3, and OCI8; a SQLSTATE **string** for Postgre and SQLSRV):
212244

213245
.. literalinclude:: queries/015.php
214246

247+
.. _database-get-last-exception:
248+
249+
$db->getLastException()
250+
-----------------------
251+
252+
.. versionadded:: 4.8.0
253+
254+
``getLastException()`` returns the typed exception that would have been
255+
thrown had ``DBDebug`` been ``true``. This is the recommended way to
256+
distinguish between failure types (e.g., a unique constraint violation vs.
257+
another database error) without enabling ``DBDebug``:
258+
259+
.. literalinclude:: queries/031.php
260+
261+
.. note:: ``getLastException()`` is reset to ``null`` at the start of every
262+
query. Inspect it immediately after the failed operation.
215263

216264
****************
217265
Prepared Queries
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use CodeIgniter\Database\Exceptions\DatabaseException;
4+
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
5+
6+
try {
7+
$db->table('users')->insert(['email' => 'duplicate@example.com']);
8+
} catch (UniqueConstraintViolationException $e) {
9+
// Duplicate key — handle gracefully
10+
} catch (DatabaseException $e) {
11+
// Other database error
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
4+
5+
$inserted = $db->table('users')->insert(['email' => 'duplicate@example.com']);
6+
7+
if (! $inserted && $db->getLastException() instanceof UniqueConstraintViolationException) {
8+
// Handle duplicate key violation
9+
}

user_guide_src/source/general/errors.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ or when it is temporarily lost:
204204

205205
This provides an exit code of 8.
206206

207+
UniqueConstraintViolationException
208+
----------------------------------
209+
210+
.. versionadded:: 4.8.0
211+
212+
``UniqueConstraintViolationException`` extends ``DatabaseException`` and is thrown when a query
213+
fails due to a duplicate key or unique constraint violation. It is supported by all database drivers.
214+
215+
.. literalinclude:: errors/019.php
216+
217+
See :ref:`database-unique-constraint-violation` for full usage details.
218+
207219
RedirectException
208220
-----------------
209221

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
4+
5+
try {
6+
$db->table('users')->insert(['email' => 'duplicate@example.com']);
7+
} catch (UniqueConstraintViolationException $e) {
8+
// Handle duplicate key violation
9+
}

0 commit comments

Comments
 (0)