yii\mongodb\Connection::getDatabase PHP Method

getDatabase() public method

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.
return Database database instance.
    public function getDatabase($name = null, $refresh = false)
    {
        if ($name === null) {
            $name = $this->getDefaultDatabaseName();
        }
        if ($refresh || !array_key_exists($name, $this->_databases)) {
            $this->_databases[$name] = $this->selectDatabase($name);
        }
        return $this->_databases[$name];
    }

Usage Example

Example #1
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!');
 }