Skip to content

Commit 3dffbc9

Browse files
authored
Merge pull request #8767 from kenjis/feat-resetTransStatus
feat: add BaseConnection::resetTransStatus()
2 parents 85434df + 723d9e7 commit 3dffbc9

File tree

5 files changed

+86
-6
lines changed

5 files changed

+86
-6
lines changed

system/Database/BaseConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,16 @@ public function transRollback(): bool
897897
return false;
898898
}
899899

900+
/**
901+
* Reset transaction status - to restart transactions after strict mode failure
902+
*/
903+
public function resetTransStatus(): static
904+
{
905+
$this->transStatus = true;
906+
907+
return $this;
908+
}
909+
900910
/**
901911
* Begin Transaction
902912
*/

tests/system/Database/Live/TransactionDBDebugTrueTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,54 @@ public function testTransStrictFalse(): void
208208
$this->seeInDatabase('job', ['name' => 'Comedian']);
209209
}
210210

211+
public function testTransStrictTrueAndResetTransStatus(): void
212+
{
213+
$builder = $this->db->table('job');
214+
215+
// The first transaction group
216+
$this->db->transStart();
217+
218+
$jobData = [
219+
'name' => 'Grocery Sales',
220+
'description' => 'Discount!',
221+
];
222+
$builder->insert($jobData);
223+
224+
$this->assertTrue($this->db->transStatus());
225+
226+
// Duplicate entry '1' for key 'PRIMARY'
227+
$jobData = [
228+
'id' => 1,
229+
'name' => 'Comedian',
230+
'description' => 'Theres something in your teeth',
231+
];
232+
$builder->insert($jobData);
233+
234+
$this->assertFalse($this->db->transStatus());
235+
236+
$this->db->transComplete();
237+
238+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
239+
240+
// Resets TransStatus
241+
$this->db->resetTransStatus();
242+
243+
// The second transaction group
244+
$this->db->transStart();
245+
246+
$jobData = [
247+
'name' => 'Comedian',
248+
'description' => 'Theres something in your teeth',
249+
];
250+
$builder->insert($jobData);
251+
252+
$this->assertTrue($this->db->transStatus());
253+
254+
$this->db->transComplete();
255+
256+
$this->seeInDatabase('job', ['name' => 'Comedian']);
257+
}
258+
211259
public function testTransBegin(): void
212260
{
213261
$builder = $this->db->table('job');

user_guide_src/source/changelogs/v4.6.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ Others
135135
------
136136

137137
- Added a new configuration ``foundRows`` for MySQLi to use ``MYSQLI_CLIENT_FOUND_ROWS``.
138+
- Added the ``BaseConnection::resetTransStatus()`` method to reset the transaction
139+
status. See :ref:`transactions-resetting-transaction-status` for details.
138140

139141
Model
140142
=====

user_guide_src/source/database/transactions.rst

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ transactions.
1414

1515
.. contents::
1616
:local:
17-
:depth: 2
17+
:depth: 3
1818

1919
CodeIgniter's Approach to Transactions
2020
======================================
@@ -52,16 +52,33 @@ or failure of any given query.
5252
Strict Mode
5353
===========
5454

55-
By default, CodeIgniter runs all transactions in Strict Mode. When strict
56-
mode is enabled, if you are running multiple groups of transactions, if
57-
one group fails all subsequent groups will be rolled back. If strict mode is
58-
disabled, each group is treated independently, meaning a failure of one
59-
group will not affect any others.
55+
By default, CodeIgniter runs all transactions in Strict Mode.
56+
57+
When strict mode is enabled, if you are running multiple groups of transactions,
58+
if one group fails all subsequent groups will be rolled back.
59+
60+
If strict mode is disabled, each group is treated independently, meaning a failure
61+
of one group will not affect any others.
6062

6163
Strict Mode can be disabled as follows:
6264

6365
.. literalinclude:: transactions/002.php
6466

67+
.. _transactions-resetting-transaction-status:
68+
69+
Resetting Transaction Status
70+
----------------------------
71+
72+
.. versionadded:: 4.6.0
73+
74+
When strict mode is enabled, if one transaction fails, all subsequent transactions
75+
will be rolled back.
76+
77+
If you wan to restart transactions after a failure, you can reset the transaction
78+
status:
79+
80+
.. literalinclude:: transactions/009.php
81+
6582
.. _transactions-managing-errors:
6683

6784
Managing Errors
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$this->db->resetTransStatus();

0 commit comments

Comments
 (0)