Skip to content
PHP Ecosystem
Explore

Transaction

API reference › yii › db › Transaction

classyii\db\Transaction
Extendsyii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/db/Transaction.php
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. \Exception implements the \Throwable interface since PHP 7.0, so you can skip the part with \Exception if your app uses only PHP 7.0 and higher.

PropertyTypeDescriptionDefined by
$dbConnectionThe database connection that this transaction is associated with. Default: null.Transaction
$isActive read-onlyboolWhether this transaction is active.Transaction
$isolationLevel write-onlystringThe transaction isolation level to use for this transaction.Transaction
$level read-onlyintThe current nesting level of the transaction.Transaction
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
begin()Begins a transaction.Transaction
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
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\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
rollBack()Rolls back a transaction.Transaction
setIsolationLevel()Sets the transaction isolation level for this transaction.Transaction
ConstantValueDescriptionDefined 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
public bool $isActive

Whether this transaction is active.

Only an active transaction can commit() or rollBack().

Access

Read-only (virtual property).

public string $isolationLevel

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.

Access

Write-only (virtual property).

public function begin(string|null $isolationLevel = null): mixed

Begins a transaction.

Parameters

ParameterTypeDescription
$isolationLevelstring|nullThe [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

public function commit(): mixed

Commits a transaction.

Returns

mixed

Throws

  • Exception: if the transaction is not active
public function getIsActive(): bool

Returns a value indicating whether this transaction is active.

Returns

bool: Whether this transaction is active. Only an active transaction can commit() or rollBack().

public function getLevel(): int

Returns

int: The current nesting level of the transaction.

Since

2.0.8

public function rollBack(): mixed

Rolls back a transaction.

Returns

mixed

public function setIsolationLevel(string $level): mixed

Sets 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

ParameterTypeDescription
$levelstringThe 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