Inheritance | yii\db\Transaction » yii\base\Object |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/Transaction.php |
Transaction represents a DB transaction.
It is usually created by calling yii\db\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.
Property | Type | Description | Defined By |
---|---|---|---|
$db | yii\db\Connection | The database connection that this transaction is associated with. | yii\db\Transaction |
$isActive | boolean | Whether this transaction is active. | yii\db\Transaction |
$isolationLevel | string | The transaction isolation level to use for this transaction. | yii\db\Transaction |
$level | integer | The current nesting level of the transaction. | yii\db\Transaction |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Object |
__construct() | Constructor. | yii\base\Object |
__get() | Returns the value of an object property. | yii\base\Object |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Object |
__set() | Sets value of an object property. | yii\base\Object |
__unset() | Sets an object property to null. | yii\base\Object |
begin() | Begins a transaction. | yii\db\Transaction |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Object |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Object |
className() | Returns the fully qualified name of this class. | yii\base\Object |
commit() | Commits a transaction. | yii\db\Transaction |
getIsActive() | Returns a value indicating whether this transaction is active. | yii\db\Transaction |
getLevel() | yii\db\Transaction | |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Object |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\Object |
init() | Initializes the object. | yii\base\Object |
rollBack() | Rolls back a transaction. | yii\db\Transaction |
setIsolationLevel() | Sets the transaction isolation level for this transaction. | yii\db\Transaction |
Constant | Value | Description | Defined By |
---|---|---|---|
READ_COMMITTED | 'READ COMMITTED' | A constant representing the transaction isolation level READ COMMITTED . | yii\db\Transaction |
READ_UNCOMMITTED | 'READ UNCOMMITTED' | A constant representing the transaction isolation level READ UNCOMMITTED . | yii\db\Transaction |
REPEATABLE_READ | 'REPEATABLE READ' | A constant representing the transaction isolation level REPEATABLE READ . | yii\db\Transaction |
SERIALIZABLE | 'SERIALIZABLE' | A constant representing the transaction isolation level SERIALIZABLE . | yii\db\Transaction |
The database connection that this transaction is associated with.
public yii\db\Connection $db = null
Whether this transaction is active. Only an active transaction can commit() or rollBack().
public boolean getIsActive ( )
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
.
public void setIsolationLevel ( $level )
The current nesting level of the transaction.
public integer getLevel ( )
Begins a transaction.
public void begin ( $isolationLevel = null ) | ||
---|---|---|
$isolationLevel | string|null |
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
|
throws | yii\base\InvalidConfigException |
if $db is |
Commits a transaction.
public void commit ( ) | ||
---|---|---|
throws | yii\db\Exception |
if the transaction is not active |
Returns a value indicating whether this transaction is active.
public boolean getIsActive ( ) | ||
---|---|---|
return | boolean |
Whether this transaction is active. Only an active transaction can commit() or rollBack(). |
public integer getLevel ( ) | ||
---|---|---|
return | integer |
The current nesting level of the transaction. |
Rolls back a transaction.
public void rollBack ( ) | ||
---|---|---|
throws | yii\db\Exception |
if the transaction is not active |
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().
See also http://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.
public void setIsolationLevel ( $level ) | ||
---|---|---|
$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 |
throws | yii\db\Exception |
if the transaction is not active |
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-db-transaction.html