Class yii\mongodb\ClientSession

Inheritanceyii\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:

Public Properties

Hide inherited 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

Hide inherited 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

Hide inherited properties

$db public property

The database connection that this transaction is associated with.

$id public property
public string $id null
$inTransaction public property
public boolean $inTransaction null
$mongoSession public property

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.

public \yii\mongodb\MongoDB\Driver\Session $mongoSession null
$transaction public property

Returns current transaction.

Method Details

Hide inherited methods

end() public method

Ends the current session.

public void end ( )

                public function end()
{
    $this->mongoSession->endSession();
    $this->db->trigger(Connection::EVENT_END_SESSION);
}

            
getId() public method

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'];
}

            
getInTransaction() public method

Returns true if the transaction is in progress

public boolean getInTransaction ( )

                public function getInTransaction()
{
    return $this->mongoSession->isInTransaction();
}

            
getTransaction() public method

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;
}

            
prepareOptions() public static method

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']
                        )
                ;
            }
        }
    }
}

            
start() public static method

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;
}