Class yii\mongodb\Transaction
| Inheritance | yii\mongodb\Transaction » yii\base\BaseObject |
|---|---|
| Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/src/Transaction.php |
In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for multi-document transactions for many practical use cases.
For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.
See also:
- https://docs.mongodb.com/core/transactions/ Note : At least 1.5 mongodb php driver version is supported. Note : At least 4.0 MongoDB version is supported.
- https://docs.mongodb.com/ecosystem/drivers/php/#mongodb-compatibility Note : Nested transaction not supported.
- https://docs.mongodb.com/manual/core/transactions/#transactions-and-sessions
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $clientSession | \yii\mongodb\MongoDB\Driver\Session | Class represents a client session and Commands, queries, and write operations may then be associated the session. | yii\mongodb\Transaction |
| $isActive | boolean | Whether this transaction is active. | yii\mongodb\Transaction |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| commit() | Commit a transaction. | yii\mongodb\Transaction |
| getIsActive() | Returns a value indicating whether this transaction is active. | yii\mongodb\Transaction |
| getState() | Returns the transaction state. | yii\mongodb\Transaction |
| rollBack() | Rolls back a transaction. | yii\mongodb\Transaction |
| start() | Start a transaction if session is not in transaction process. | yii\mongodb\Transaction |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| yiiBeginProfile() | Begin profile if enableProfiling property is enable in yii\mongodb\Connection |
yii\mongodb\Transaction |
| yiiDebug() | Set debug message if enableLogging property is enable in yii\mongodb\Connection |
yii\mongodb\Transaction |
| yiiEndProfile() | End profile if enableProfiling property is enable in yii\mongodb\Connection |
yii\mongodb\Transaction |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| STATE_ABORTED | 'aborted' | yii\mongodb\Transaction | |
| STATE_COMMITTED | 'committed' | yii\mongodb\Transaction | |
| STATE_NONE | 'none' | yii\mongodb\Transaction | |
| STATE_STARTING | 'starting' | yii\mongodb\Transaction |
Property Details
Class represents a client session and Commands, queries, and write operations may then be associated the session.
See also https://www.php.net/manual/en/class.mongodb-driver-session.php.
Whether this transaction is active. Only an active transaction can commit() or rollBack().
Method Details
Commit a transaction.
See also https://www.php.net/manual/en/mongodb-driver-session.committransaction.php.
| public mixed commit ( ) |
public function commit()
{
$this->yiiDebug('Committing mongodb transaction ...', __METHOD__);
$this->clientSession->mongoSession->commitTransaction();
$this->yiiEndProfile('mongodb > start transaction(session id => ' . $this->clientSession->getId() . ')');
$this->yiiDebug('Commit mongodb transaction.', __METHOD__);
$this->clientSession->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
}
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 function getIsActive()
{
return $this->clientSession->db->getIsActive() && $this->clientSession->getInTransaction();
}
Returns the transaction state.
| public mixed getState ( ) |
public function getState()
{
return $this->clientSession->mongoSession->getTransactionState();
}
Rolls back a transaction.
See also https://www.php.net/manual/en/mongodb-driver-session.aborttransaction.php.
| public mixed rollBack ( ) |
public function rollBack()
{
$this->yiiDebug('Rolling back mongodb transaction ...', __METHOD__);
$this->clientSession->mongoSession->abortTransaction();
$this->yiiEndProfile('mongodb > start transaction(session id => ' . $this->clientSession->getId() . ')');
$this->yiiDebug('Roll back mongodb transaction.', __METHOD__);
$this->clientSession->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
}
Start a transaction if session is not in transaction process.
See also:
| public mixed start ( array $transactionOptions = [] ) | ||
| $transactionOptions | array |
Options can be passed as argument to this method. Each element in this options array overrides the corresponding option from the "sessionOptions" option, if set when starting the session with ClientSession::start(). |
public function start($transactionOptions = [])
{
Command::prepareManagerOptions($transactionOptions);
$this->yiiDebug('Starting mongodb transaction ...', __METHOD__);
if ($this->clientSession->getInTransaction()) {
throw new Exception('Nested transaction not supported');
}
$this->clientSession->db->trigger(Connection::EVENT_START_TRANSACTION);
$this->yiiBeginProfile('mongodb > start transaction(session id => ' . $this->clientSession->getId() . ')');
$this->clientSession->mongoSession->startTransaction($transactionOptions);
$this->yiiDebug('MongoDB transaction started.', __METHOD__);
}
Begin profile if enableProfiling property is enable in yii\mongodb\Connection
| protected mixed yiiBeginProfile ( mixed $token, mixed $category = 'mongodb' ) | ||
| $token | mixed | |
| $category | mixed | |
protected function yiiBeginProfile($token, $category = 'mongodb')
{
if ($this->clientSession->db->enableProfiling) {
Yii::beginProfile($token,$category);
}
}
Set debug message if enableLogging property is enable in yii\mongodb\Connection
| protected mixed yiiDebug ( mixed $message, mixed $category = 'mongodb' ) | ||
| $message | mixed | |
| $category | mixed | |
protected function yiiDebug($message, $category = 'mongodb')
{
if ($this->clientSession->db->enableLogging) {
Yii::debug($message,$category);
}
}
End profile if enableProfiling property is enable in yii\mongodb\Connection
| protected mixed yiiEndProfile ( mixed $token, mixed $category = 'mongodb' ) | ||
| $token | mixed | |
| $category | mixed | |
protected function yiiEndProfile($token, $category = 'mongodb')
{
if ($this->clientSession->db->enableProfiling) {
Yii::endProfile($token,$category);
}
}