PHP Класс yii\mongodb\Connection

Connection works together with Database and Collection to provide data access to the Mongo database. They are wrappers of the [[MongoDB PHP extension]](http://us1.php.net/manual/en/book.mongo.php). To establish a DB connection, set [[dsn]] and then call Connection::open to be true. The following example shows how to create a Connection instance and establish the DB connection: ~~~ $connection = new \yii\mongodb\Connection([ 'dsn' => $dsn, ]); $connection->open(); ~~~ After the Mongo connection is established, one can access Mongo databases and collections: ~~~ $database = $connection->getDatabase('my_mongo_db'); $collection = $database->getCollection('customer'); $collection->insert(['name' => 'John Smith', 'status' => 1]); ~~~ You can work with several different databases at the same server using this class. However, while it is unlikely your application will actually need it, the Connection class provides ability to use [[defaultDatabaseName]] as well as a shortcut method Connection::getCollection to retrieve a particular collection instance: ~~~ get collection 'customer' from default database: $collection = $connection->getCollection('customer'); get collection 'customer' from database 'mydatabase': $collection = $connection->getCollection(['mydatabase', 'customer']); ~~~ Connection is often used as an application component and configured in the application configuration like the following: ~~~ [ 'components' => [ 'mongodb' => [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase', ], ], ] ~~~
С версии: 2.0
Автор: Paul Klimov ([email protected])
Наследование: extends yii\base\Component
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$driverOptions options for the MongoDB driver. Any driver-specific options not included in MongoDB connection string specification.
$dsn host:port Correct syntax is: mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname] For example: mongodb://localhost:27017 mongodb://developer:password@localhost:27017 mongodb://developer:password@localhost:27017/mydatabase
$enableLogging whether to log command and query executions. When enabled this option may reduce performance. MongoDB commands may contain large data, consuming both CPU and memory. It makes sense to disable this option in the production environment.
$enableProfiling whether to enable profiling the commands and queries being executed. This option will have no effect in case [[enableLogging]] is disabled.
$fileStreamProtocol name of the protocol, which should be used for the GridFS stream wrapper. Only alphanumeric values are allowed: do not use any URL special characters, such as '/', '&', ':' etc.
$fileStreamWrapperClass name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol.
$manager MongoDB driver manager.
$options connection options. For example: php [ 'socketTimeoutMS' => 1000, // how long a send or receive on a socket can take before timing out 'ssl' => true // initiate the connection with TLS/SSL ]
$typeMap type map to use for BSON unserialization. Note: default type map will be automatically merged into this field, possibly overriding user-defined values.

Открытые методы

Метод Описание
close ( ) Closes the currently active DB connection.
createCommand ( array $document = [], string | null $databaseName = null ) : Command Creates MongoDB command.
getCollection ( string | array $name, boolean $refresh = false ) : Collection Returns the MongoDB collection with the given name.
getDatabase ( string | null $name = null, boolean $refresh = false ) : Database Returns the MongoDB database with the given name.
getDefaultDatabaseName ( ) : string Returns default database name, if it is not set, attempts to determine it from [[dsn]] value.
getFileCollection ( string | array $prefix = 'fs', boolean $refresh = false ) : Collection Returns the MongoDB GridFS collection.
getIsActive ( ) : boolean Returns a value indicating whether the Mongo connection is established.
getLogBuilder ( ) : yii\mongodb\LogBuilder Returns log builder for this connection.
getQueryBuilder ( ) : QueryBuilder Returns the query builder for the this MongoDB connection.
open ( ) Establishes a Mongo connection.
registerFileStreamWrapper ( boolean $force = false ) : string Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
setDefaultDatabaseName ( string $name ) Sets default database name.
setLogBuilder ( array | string | yii\mongodb\LogBuilder $logBuilder ) Sets log builder used for this connection.
setQueryBuilder ( QueryBuilder | array | string | null $queryBuilder ) Sets the query builder for the this MongoDB connection.

Защищенные методы

Метод Описание
initConnection ( ) Initializes the DB connection.
selectDatabase ( string $name ) : Database Selects the database with given name.

Описание методов

close() публичный Метод

It does nothing if the connection is already closed.
public close ( )

createCommand() публичный Метод

Creates MongoDB command.
С версии: 2.1
public createCommand ( array $document = [], string | null $databaseName = null ) : Command
$document array command document contents.
$databaseName string | null database name, if not set [[defaultDatabaseName]] will be used.
Результат Command command instance.

getCollection() публичный Метод

Returns the MongoDB collection with the given name.
public getCollection ( string | array $name, boolean $refresh = false ) : Collection
$name string | array collection name. If string considered as the name of the collection inside the default database. If array - first element considered as the name of the database, second - as name of collection inside that database
$refresh boolean whether to reload the collection instance even if it is found in the cache.
Результат Collection Mongo collection instance.

getDatabase() публичный Метод

Returns the MongoDB database with the given name.
public getDatabase ( string | null $name = null, boolean $refresh = false ) : Database
$name string | null database name, if null default one will be used.
$refresh boolean whether to reestablish the database connection even, if it is found in the cache.
Результат Database database instance.

getDefaultDatabaseName() публичный Метод

Returns default database name, if it is not set, attempts to determine it from [[dsn]] value.
public getDefaultDatabaseName ( ) : string
Результат string default database name

getFileCollection() публичный Метод

Returns the MongoDB GridFS collection.
public getFileCollection ( string | array $prefix = 'fs', boolean $refresh = false ) : Collection
$prefix string | array collection prefix. If string considered as the prefix of the GridFS collection inside the default database. If array - first element considered as the name of the database, second - as prefix of the GridFS collection inside that database, if no second element present default "fs" prefix will be used.
$refresh boolean whether to reload the collection instance even if it is found in the cache.
Результат yii\mongodb\file\Collection Mongo GridFS collection instance.

getIsActive() публичный Метод

Returns a value indicating whether the Mongo connection is established.
public getIsActive ( ) : boolean
Результат boolean whether the Mongo connection is established

getLogBuilder() публичный Метод

Returns log builder for this connection.
С версии: 2.1
public getLogBuilder ( ) : yii\mongodb\LogBuilder
Результат yii\mongodb\LogBuilder the log builder for this connection.

getQueryBuilder() публичный Метод

Returns the query builder for the this MongoDB connection.
С версии: 2.1
public getQueryBuilder ( ) : QueryBuilder
Результат QueryBuilder the query builder for the this MongoDB connection.

initConnection() защищенный Метод

This method is invoked right after the DB connection is established. The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
protected initConnection ( )

open() публичный Метод

It does nothing if a MongoDB connection has already been established.
public open ( )

registerFileStreamWrapper() публичный Метод

Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
public registerFileStreamWrapper ( boolean $force = false ) : string
$force boolean whether to enforce registration even wrapper has been already registered.
Результат string registered stream protocol name.

selectDatabase() защищенный Метод

Selects the database with given name.
protected selectDatabase ( string $name ) : Database
$name string database name.
Результат Database database instance.

setDefaultDatabaseName() публичный Метод

Sets default database name.
public setDefaultDatabaseName ( string $name )
$name string default database name.

setLogBuilder() публичный Метод

Sets log builder used for this connection.
С версии: 2.1
public setLogBuilder ( array | string | yii\mongodb\LogBuilder $logBuilder )
$logBuilder array | string | yii\mongodb\LogBuilder the log builder for this connection.

setQueryBuilder() публичный Метод

Sets the query builder for the this MongoDB connection.
С версии: 2.1
public setQueryBuilder ( QueryBuilder | array | string | null $queryBuilder )
$queryBuilder QueryBuilder | array | string | null the query builder for this MongoDB connection.

Описание свойств

$driverOptions публичное свойство

options for the MongoDB driver. Any driver-specific options not included in MongoDB connection string specification.
См. также: http://php.net/manual/en/mongodb-driver-manager.construct.php
public $driverOptions

$dsn публичное свойство

host:port Correct syntax is: mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname] For example: mongodb://localhost:27017 mongodb://developer:password@localhost:27017 mongodb://developer:password@localhost:27017/mydatabase
public $dsn

$enableLogging публичное свойство

whether to log command and query executions. When enabled this option may reduce performance. MongoDB commands may contain large data, consuming both CPU and memory. It makes sense to disable this option in the production environment.
С версии: 2.1
public $enableLogging

$enableProfiling публичное свойство

whether to enable profiling the commands and queries being executed. This option will have no effect in case [[enableLogging]] is disabled.
С версии: 2.1
public $enableProfiling

$fileStreamProtocol публичное свойство

name of the protocol, which should be used for the GridFS stream wrapper. Only alphanumeric values are allowed: do not use any URL special characters, such as '/', '&', ':' etc.
См. также: yii\mongodb\file\StreamWrapper
С версии: 2.1
public $fileStreamProtocol

$fileStreamWrapperClass публичное свойство

name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol.
С версии: 2.1
public $fileStreamWrapperClass

$manager публичное свойство

MongoDB driver manager.
С версии: 2.1
public $manager

$options публичное свойство

connection options. For example: php [ 'socketTimeoutMS' => 1000, // how long a send or receive on a socket can take before timing out 'ssl' => true // initiate the connection with TLS/SSL ]
См. также: https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
public $options

$typeMap публичное свойство

type map to use for BSON unserialization. Note: default type map will be automatically merged into this field, possibly overriding user-defined values.
См. также: http://php.net/manual/en/mongodb-driver-cursor.settypemap.php
С версии: 2.1
public $typeMap