没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\db\Transaction

继承yii\db\Transaction » yii\base\BaseObject
实现yii\base\Configurable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/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.

公共属性

隐藏继承的属性

属性类型描述被定义在
$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

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. 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. yii\db\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() Returns the fully qualified name of this class. yii\base\BaseObject
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\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. yii\db\Transaction
setIsolationLevel() Sets the transaction isolation level for this transaction. yii\db\Transaction

常量

隐藏继承的常量

常量描述被定义在
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

属性详情

$db 公共 属性

The database connection that this transaction is associated with.

public yii\db\Connection $db null
$isActive 公共 只读 属性

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

public boolean getIsActive ( )
$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.

public void setIsolationLevel ( $level )
$level 公共 只读 属性

The current nesting level of the transaction.

public integer getLevel ( )

方法详情

begin() 公共 方法

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 SET TRANSACTION ISOLATION LEVEL. If not specified (null) the isolation level will not be set explicitly and the DBMS default will be used.

Note: This setting does not work for PostgreSQL, where setting the isolation level before the transaction has no effect. You have to call setIsolationLevel() in this case after the transaction has started.

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.

Starting from version 2.0.16, this method throws exception when beginning nested transaction and underlying DBMS does not support savepoints.

throws yii\base\InvalidConfigException

if $db is null

throws yii\base\NotSupportedException

if the DBMS does not support nested transactions

throws yii\db\Exception

if DB connection fails

commit() 公共 方法

Commits a transaction.

public void commit()
throws yii\db\Exception

if the transaction is not active

getIsActive() 公共 方法

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().

getLevel() 公共 方法 (自版本 2.0.8 可用)

public integer getLevel()
return integer

The current nesting level of the transaction.

rollBack() 公共 方法

Rolls back a transaction.

public void rollBack()
setIsolationLevel() 公共 方法

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().

参见 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 SET TRANSACTION ISOLATION LEVEL.

throws yii\db\Exception

if the transaction is not active