mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-08 17:07:33 +08:00
Mongo Connection updated allowing to fetch default database name from options.
This commit is contained in:
@ -85,7 +85,9 @@ class Connection extends Component
|
|||||||
protected function fetchDefaultDatabaseName()
|
protected function fetchDefaultDatabaseName()
|
||||||
{
|
{
|
||||||
if ($this->defaultDatabaseName === null) {
|
if ($this->defaultDatabaseName === null) {
|
||||||
if (preg_match('/^mongodb:\\/\\/.+\\/(.+)$/s', $this->dsn, $matches)) {
|
if (isset($this->options['db'])) {
|
||||||
|
$this->defaultDatabaseName = $this->options['db'];
|
||||||
|
} elseif (preg_match('/^mongodb:\\/\\/.+\\/(.+)$/s', $this->dsn, $matches)) {
|
||||||
$this->defaultDatabaseName = $matches[1];
|
$this->defaultDatabaseName = $matches[1];
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidConfigException("Unable to determine default database name from dsn.");
|
throw new InvalidConfigException("Unable to determine default database name from dsn.");
|
||||||
|
|||||||
@ -70,6 +70,12 @@ class ConnectionTest extends MongoTestCase
|
|||||||
$database = $connection->getDatabase();
|
$database = $connection->getDatabase();
|
||||||
$this->assertTrue($database instanceof Database, 'Unable to get default database!');
|
$this->assertTrue($database instanceof Database, 'Unable to get default database!');
|
||||||
|
|
||||||
|
$connection = new Connection();
|
||||||
|
$connection->dsn = $this->mongoConfig['dsn'];
|
||||||
|
$connection->options = ['db' => $this->mongoConfig['defaultDatabaseName']];
|
||||||
|
$database = $connection->getDatabase();
|
||||||
|
$this->assertTrue($database instanceof Database, 'Unable to determine default database from options!');
|
||||||
|
|
||||||
$connection = new Connection();
|
$connection = new Connection();
|
||||||
$connection->dsn = $this->mongoConfig['dsn'] . '/' . $this->mongoConfig['defaultDatabaseName'];
|
$connection->dsn = $this->mongoConfig['dsn'] . '/' . $this->mongoConfig['defaultDatabaseName'];
|
||||||
$database = $connection->getDatabase();
|
$database = $connection->getDatabase();
|
||||||
|
|||||||
@ -77,9 +77,7 @@ class MongoTestCase extends TestCase
|
|||||||
}
|
}
|
||||||
$db = new Connection;
|
$db = new Connection;
|
||||||
$db->dsn = $this->mongoConfig['dsn'];
|
$db->dsn = $this->mongoConfig['dsn'];
|
||||||
if (isset($this->mongoConfig['defaultDatabaseName'])) {
|
|
||||||
$db->defaultDatabaseName = $this->mongoConfig['defaultDatabaseName'];
|
$db->defaultDatabaseName = $this->mongoConfig['defaultDatabaseName'];
|
||||||
}
|
|
||||||
if (isset($this->mongoConfig['options'])) {
|
if (isset($this->mongoConfig['options'])) {
|
||||||
$db->options = $this->mongoConfig['options'];
|
$db->options = $this->mongoConfig['options'];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user