Transaction
extends BaseObject
in package
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.
Tags
Table of Contents
Constants
- READ_COMMITTED = 'READ COMMITTED'
- A constant representing the transaction isolation level `READ COMMITTED`.
- READ_UNCOMMITTED = 'READ UNCOMMITTED'
- A constant representing the transaction isolation level `READ UNCOMMITTED`.
- REPEATABLE_READ = 'REPEATABLE READ'
- A constant representing the transaction isolation level `REPEATABLE READ`.
- SERIALIZABLE = 'SERIALIZABLE'
- A constant representing the transaction isolation level `SERIALIZABLE`.
Properties
- $db : Connection
- $isActive : bool
- $isolationLevel : string
- $level : int
- $_level : int
Methods
- __call() : mixed
- Calls the named method which is not a class method.
- __construct() : mixed
- Constructor.
- __get() : mixed
- Returns the value of an object property.
- __isset() : bool
- Checks if a property is set, i.e. defined and not null.
- __set() : mixed
- Sets value of an object property.
- __unset() : mixed
- Sets an object property to null.
- begin() : mixed
- Begins a transaction.
- canGetProperty() : bool
- Returns a value indicating whether a property can be read.
- canSetProperty() : bool
- Returns a value indicating whether a property can be set.
- className() : string
- Returns the fully qualified name of this class.
- commit() : mixed
- Commits a transaction.
- getIsActive() : bool
- Returns a value indicating whether this transaction is active.
- getLevel() : int
- hasMethod() : bool
- Returns a value indicating whether a method is defined.
- hasProperty() : bool
- Returns a value indicating whether a property is defined.
- init() : mixed
- Initializes the object.
- rollBack() : mixed
- Rolls back a transaction.
- setIsolationLevel() : mixed
- Sets the transaction isolation level for this transaction.
Constants
READ_COMMITTED
A constant representing the transaction isolation level `READ COMMITTED`.
public
mixed
READ_COMMITTED
= 'READ COMMITTED'
Tags
READ_UNCOMMITTED
A constant representing the transaction isolation level `READ UNCOMMITTED`.
public
mixed
READ_UNCOMMITTED
= 'READ UNCOMMITTED'
Tags
REPEATABLE_READ
A constant representing the transaction isolation level `REPEATABLE READ`.
public
mixed
REPEATABLE_READ
= 'REPEATABLE READ'
Tags
SERIALIZABLE
A constant representing the transaction isolation level `SERIALIZABLE`.
public
mixed
SERIALIZABLE
= 'SERIALIZABLE'
Tags
Properties
$db
public
Connection
$db
the database connection that this transaction is associated with.
$isActive read-only
public
bool
$isActive
Whether this transaction is active. Only an active transaction can [[commit()]] or [[rollBack()]].
$isolationLevel write-only
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
.
$level read-only
public
int
$level
The current nesting level of the transaction.
$_level
private
int
$_level
= 0
the nesting level of the transaction. 0 means the outermost level.
Methods
__call()
Calls the named method which is not a class method.
public
__call(string $name, array<string|int, mixed> $params) : mixed
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
Parameters
- $name : string
-
the method name
- $params : array<string|int, mixed>
-
method parameters
Tags
Return values
mixed —the method return value
__construct()
Constructor.
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
The default implementation does two things:
- Initializes the object with the given configuration
$config
. - Call [[init()]].
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here. - call the parent implementation at the end of the constructor.
Parameters
- $config : array<string|int, mixed> = []
-
name-value pairs that will be used to initialize the object properties
__get()
Returns the value of an object property.
public
__get(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
Parameters
- $name : string
-
the property name
Tags
Return values
mixed —the property value
__isset()
Checks if a property is set, i.e. defined and not null.
public
__isset(string $name) : bool
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property)
.
Note that if the property is not defined, false will be returned.
Parameters
- $name : string
-
the property name or the event name
Tags
Return values
bool —whether the named property is set (not null).
__set()
Sets value of an object property.
public
__set(string $name, mixed $value) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
Parameters
- $name : string
-
the property name or the event name
- $value : mixed
-
the property value
Tags
__unset()
Sets an object property to null.
public
__unset(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
Parameters
- $name : string
-
the property name
Tags
begin()
Begins a transaction.
public
begin([string|null $isolationLevel = null ]) : mixed
Parameters
- $isolationLevel : string|null = 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.
Tags
canGetProperty()
Returns a value indicating whether a property can be read.
public
canGetProperty(string $name[, bool $checkVars = true ]) : bool
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be read
canSetProperty()
Returns a value indicating whether a property can be set.
public
canSetProperty(string $name[, bool $checkVars = true ]) : bool
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be written
className()
Returns the fully qualified name of this class.
public
static className() : string
Tags
Return values
string —the fully qualified name of this class.
commit()
Commits a transaction.
public
commit() : mixed
Tags
getIsActive()
Returns a value indicating whether this transaction is active.
public
getIsActive() : bool
Return values
bool —whether this transaction is active. Only an active transaction can [[commit()]] or [[rollBack()]].
getLevel()
public
getLevel() : int
Tags
Return values
int —The current nesting level of the transaction.
hasMethod()
Returns a value indicating whether a method is defined.
public
hasMethod(string $name) : bool
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
Parameters
- $name : string
-
the method name
Return values
bool —whether the method is defined
hasProperty()
Returns a value indicating whether a property is defined.
public
hasProperty(string $name[, bool $checkVars = true ]) : bool
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property is defined
init()
Initializes the object.
public
init() : mixed
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
rollBack()
Rolls back a transaction.
public
rollBack() : mixed
setIsolationLevel()
Sets the transaction isolation level for this transaction.
public
setIsolationLevel(string $level) : mixed
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
- $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
.