Transaction
API reference › yii › db › Transaction
1 subclass
Transaction represents a DB transaction.
It is usually created by calling Connection::beginTransaction().
The following code is a typical example of using transactions (note that some DBMS may not support transactions):
$transaction = $connection->beginTransaction();try { $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); //.... other SQL executions $transaction->commit();} catch (\Exception $e) { $transaction->rollBack(); throw $e;} catch (\Throwable $e) { $transaction->rollBack(); throw $e;}Note: in the above code we have two catch-blocks for compatibility with PHP 5.x and PHP 7.x.
\Exceptionimplements the\Throwableinterface since PHP 7.0, so you can skip the part with\Exceptionif your app uses only PHP 7.0 and higher.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$db | Connection | The database connection that this transaction is associated with. Default: null. | Transaction |
$isActive read-only | bool | Whether this transaction is active. | Transaction |
$isolationLevel write-only | string | The transaction isolation level to use for this transaction. | Transaction |
$level read-only | int | The current nesting level of the transaction. | Transaction |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
begin() | Begins a transaction. | Transaction |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
commit() | Commits a transaction. | Transaction |
getIsActive() | Returns a value indicating whether this transaction is active. | Transaction |
getLevel() | Transaction | |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
rollBack() | Rolls back a transaction. | Transaction |
setIsolationLevel() | Sets the transaction isolation level for this transaction. | Transaction |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
READ_UNCOMMITTED | 'READ UNCOMMITTED' | A constant representing the transaction isolation level READ UNCOMMITTED. | Transaction |
READ_COMMITTED | 'READ COMMITTED' | A constant representing the transaction isolation level READ COMMITTED. | Transaction |
REPEATABLE_READ | 'REPEATABLE READ' | A constant representing the transaction isolation level REPEATABLE READ. | Transaction |
SERIALIZABLE | 'SERIALIZABLE' | A constant representing the transaction isolation level SERIALIZABLE. | Transaction |
Property details
Section titled “Property details”$isActive
Section titled “$isActive ”public bool $isActiveWhether this transaction is active.
Only an active transaction can
commit() or rollBack().
Access
Read-only (virtual property).
$isolationLevel
Section titled “$isolationLevel ”public string $isolationLevelThe transaction isolation level to use for this transaction.
This
can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a
string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL.
Access
Write-only (virtual property).
Method details
Section titled “Method details”begin()
Section titled “begin() ”public function begin(string|null $isolationLevel = null): mixedBegins a transaction.
Parameters
| Parameter | Type | Description |
|---|---|---|
$isolationLevel | string| | The [isolation level][] to use for this transaction. This can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL. If not specified (null) the isolation level will not be set explicitly and the DBMS default will be used. > Note: Some DBMS allow setting of the isolation level only for the whole connection so subsequent transactions may get the same isolation level even if you did not specify any. When using this feature you may need to set the isolation level for all transactions explicitly to avoid conflicting settings. At the time of this writing affected DBMS are MSSQL and SQLite. > Note: The isolation level applies to the outermost transaction only. Nested transactions are implemented with savepoints and always run at the isolation level of the outer transaction, so an $isolationLevel passed to a nested begin() call is ignored and a warning is logged (since 2.0.56). [isolation level]: https://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels Starting from version 2.0.16, this method throws exception when beginning nested transaction and underlying DBMS does not support savepoints. |
Returns
mixed
Throws
yii\base\InvalidConfigException: if$dbisnullyii\base\NotSupportedException: if the DBMS does not support nested transactionsException: if DB connection fails
getIsActive()
Section titled “getIsActive() ”public function getIsActive(): boolReturns a value indicating whether this transaction is active.
Returns
bool: Whether this transaction is active. Only an active transaction can commit() or rollBack().
getLevel()
Section titled “getLevel() ”public function getLevel(): intReturns
int: The current nesting level of the transaction.
Since
2.0.8
rollBack()
Section titled “rollBack() ”public function rollBack(): mixedRolls back a transaction.
Returns
mixed
setIsolationLevel()
Section titled “setIsolationLevel() ”public function setIsolationLevel(string $level): mixedSets the transaction isolation level for this transaction.
This method can be used to set the isolation level while the transaction is already active.
However this is not supported by all DBMS so you might rather specify the isolation level directly
when calling begin().
Parameters
| Parameter | Type | Description |
|---|---|---|
$level | string | The transaction isolation level to use for this transaction. This can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL. |
Returns
mixed
Throws
Exception: if the transaction is not active
See also