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
파일 보기 프로젝트 열기: yiisoft/yii2-mongodb 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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