Class yii\mongodb\ClientSession
Inheritance | yii\mongodb\ClientSession » yii\base\BaseObject |
---|---|
Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/src/ClientSession.php |
ClientSession represents a client session and Commands, queries, and write operations may then be associated the session.
See also:
- https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions Note : The minimum supported version of mongodb php driver is 1.4.0 Note : The minimum supported version of MongoDB server is 3.6.
- https://github.com/mongodb/mongo-php-driver/releases/tag/1.4.0
- https://docs.mongodb.com/ecosystem/drivers/php/#mongodb-compatibility
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$db | yii\mongodb\Connection | The database connection that this transaction is associated with. | yii\mongodb\ClientSession |
$id | string | yii\mongodb\ClientSession | |
$inTransaction | boolean | yii\mongodb\ClientSession | |
$mongoSession | \yii\mongodb\MongoDB\Driver\Session | Class represents a client session and Commands, queries, and write operations may then be associated the session. | yii\mongodb\ClientSession |
$transaction | yii\mongodb\Transaction | Returns current transaction. | yii\mongodb\ClientSession |
Public Methods
Method | Description | Defined By |
---|---|---|
end() | Ends the current session. | yii\mongodb\ClientSession |
getId() | Returns the logical session ID as string for this session, which may be used to identify this session's operations on the server. | yii\mongodb\ClientSession |
getInTransaction() | Returns true if the transaction is in progress | yii\mongodb\ClientSession |
getTransaction() | Gets a current transaction of session or creates a new transaction once | yii\mongodb\ClientSession |
prepareOptions() | Prepares options for some purposes | yii\mongodb\ClientSession |
start() | Starts a new mongodb session in a connection. | yii\mongodb\ClientSession |
Property Details
The database connection that this transaction is associated with.
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.
Returns current transaction.
Method Details
Ends the current session.
public void end ( ) |
public function end()
{
$this->mongoSession->endSession();
$this->db->trigger(Connection::EVENT_END_SESSION);
}
Returns the logical session ID as string for this session, which may be used to identify this session's operations on the server.
See also https://www.php.net/manual/en/mongodb-driver-session.getlogicalsessionid.php.
public string getId ( ) |
public function getId()
{
return $this->mongoSession->getLogicalSessionId()->id->jsonSerialize()['$binary'];
}
Returns true if the transaction is in progress
public boolean getInTransaction ( ) |
public function getInTransaction()
{
return $this->mongoSession->isInTransaction();
}
Gets a current transaction of session or creates a new transaction once
public yii\mongodb\Transaction getTransaction ( ) | ||
return | yii\mongodb\Transaction |
Returns current transaction |
---|
public function getTransaction()
{
if ($this->_transaction === null) {
return $this->_transaction = new Transaction(['clientSession' => $this]);
}
return $this->_transaction;
}
Prepares options for some purposes
public static void prepareOptions ( &$options ) | ||
$options |
public static function prepareOptions(&$options)
{
if (array_key_exists('defaultTransactionOptions', $options)) {
//convert readConcern
if (
array_key_exists('readConcern', $options['defaultTransactionOptions']) &&
is_string($options['defaultTransactionOptions']['readConcern'])
) {
$options['defaultTransactionOptions']['readConcern'] = new ReadConcern($options['defaultTransactionOptions']['readConcern']);
}
//convert writeConcern
if (array_key_exists('writeConcern',$options['defaultTransactionOptions'])) {
if (
is_string($options['defaultTransactionOptions']['writeConcern']) ||
is_int($options['defaultTransactionOptions']['writeConcern'])
) {
$options['defaultTransactionOptions']['writeConcern'] = new WriteConcern($options['defaultTransactionOptions']['writeConcern']);
} elseif (is_array($options['defaultTransactionOptions']['writeConcern'])) {
$options['defaultTransactionOptions']['writeConcern'] =
(new \ReflectionClass('\MongoDB\Driver\WriteConcern'))
->newInstanceArgs(
$options['defaultTransactionOptions']['writeConcern']
)
;
}
}
//Convert readPreference
if (array_key_exists('readPreference',$options['defaultTransactionOptions'])) {
if (is_string($options['defaultTransactionOptions']['readPreference'])) {
$options['defaultTransactionOptions']['readPreference'] = new ReadPreference($options['defaultTransactionOptions']['readPreference']);
} else if(is_array($options['defaultTransactionOptions']['readPreference'])) {
$options['defaultTransactionOptions']['readPreference'] =
(new \ReflectionClass('\MongoDB\Driver\ReadPreference'))
->newInstanceArgs(
$options['defaultTransactionOptions']['readPreference']
)
;
}
}
}
}
Starts a new mongodb session in a connection.
public static yii\mongodb\ClientSession start ( $db, $sessionOptions = [] ) | ||
$db | yii\mongodb\Connection | |
$sessionOptions | array |
Creates a ClientSession for the given options {@see https://www.php.net/manual/en/mongodb-driver-manager.startsession.php#refsect1-mongodb-driver-manager.startsession-parameters} |
return | yii\mongodb\ClientSession |
Returns new session base on a session options for the given connection |
---|
public static function start($db, $sessionOptions = [])
{
self::prepareOptions($sessionOptions);
if ($db->enableProfiling) {
Yii::debug('Starting mongodb session ...', __METHOD__);
}
$db->trigger(Connection::EVENT_START_SESSION);
$newSession = new self([
'db' => $db,
'mongoSession' => $db->manager->startSession($sessionOptions),
]);
if ($db->enableProfiling) {
Yii::debug('MongoDB session started.', __METHOD__);
}
return $newSession;
}