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', ], ], ] ~~~
Since: 2.0
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends yii\base\Component
 /**
  * Initializes the application component.
  * This method overrides the parent implementation by establishing the database connection.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
     $this->db->getCollection($this->itemTable)->createIndex(['name' => 1], ['unique' => true]);
     $this->db->getCollection($this->ruleTable)->createIndex(['name' => 1], ['unique' => true]);
 }
Exemple #2
0
 /**
  * @param  boolean                 $reset whether to clean up the test database
  * @param  boolean                 $open  whether to open test database
  * @return \yii\mongodb\Connection
  */
 public function getConnection($reset = false, $open = true)
 {
     if (!$reset && $this->mongodb) {
         return $this->mongodb;
     }
     $db = new Connection();
     $db->dsn = $this->mongoDbConfig['dsn'];
     $db->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
     if (isset($this->mongoDbConfig['options'])) {
         $db->options = $this->mongoDbConfig['options'];
     }
     if ($open) {
         $db->open();
     }
     $this->mongodb = $db;
     return $db;
 }
Exemple #3
0
 /**
  * @depends testGetDatabase
  */
 public function testGetDefaultDatabase()
 {
     $connection = new Connection();
     $connection->dsn = $this->mongoDbConfig['dsn'];
     $connection->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
     $database = $connection->getDatabase();
     $this->assertTrue($database instanceof Database, 'Unable to get default database!');
     $connection = new Connection();
     $connection->dsn = $this->mongoDbConfig['dsn'];
     $connection->options = ['db' => $this->mongoDbConfig['defaultDatabaseName']];
     $database = $connection->getDatabase();
     $this->assertTrue($database instanceof Database, 'Unable to determine default database from options!');
     $connection = new Connection();
     $connection->dsn = $this->mongoDbConfig['dsn'] . '/' . $this->mongoDbConfig['defaultDatabaseName'];
     $database = $connection->getDatabase();
     $this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!');
 }
 /**
  * Initializes the DbMessageSource component.
  * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
  * Configured [[cache]] component would also be initialized.
  *
  * @throws InvalidConfigException if [[db]] is invalid or [[cache]] is invalid.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
     if ($this->enableCaching) {
         $this->cache = Instance::ensure($this->cache, Cache::className());
     }
 }
Exemple #5
0
 protected function doBatchSave($name, $rows)
 {
     \Yii::trace("Batch save to '{$name}'", __METHOD__);
     $this->db->getCollection($name)->batchInsert($rows);
 }
Exemple #6
0
 /**
  * Initializes the Session component.
  * This method will initialize the [[db]] property to make sure it refers to a valid MongoDB connection.
  * @throws InvalidConfigException if [[db]] is invalid.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
 }
 /**
  * Fetches associated file collection from stream options.
  * @return Collection file collection instance.
  * @throws InvalidConfigException on invalid stream options.
  */
 private function fetchCollection()
 {
     $contextOptions = $this->getContextOptions();
     if (isset($contextOptions[$this->protocol]['collection'])) {
         $collection = $contextOptions[$this->protocol]['collection'];
         if ($collection instanceof Collection) {
             throw new InvalidConfigException('"collection" context option should be an instance of "' . Collection::className() . '"');
         }
         return $collection;
     }
     if (isset($contextOptions[$this->protocol]['db'])) {
         $connection = $contextOptions[$this->protocol]['db'];
     } else {
         $connection = 'mongodb';
     }
     /* @var $connection Connection */
     $connection = Instance::ensure($connection, Connection::className());
     list($databaseName, $collectionPrefix) = explode('.', $this->namespace, 2);
     return $connection->getDatabase($databaseName)->getFileCollection($collectionPrefix);
 }