Skip to content

Commit 76a5dbc

Browse files
committed
bugfix: failover to database configuration values if no ability to get database from DSN
1 parent 068a0af commit 76a5dbc

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Jenssegers/Mongodb/Connection.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(array $config)
4141
$this->connection = $this->createConnection($dsn, $config, $options);
4242

4343
// Select database
44-
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn));
44+
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database']));
4545

4646
$this->useDefaultPostProcessor();
4747

@@ -190,7 +190,7 @@ protected function getHostDsn(array $config)
190190
}
191191

192192
// Check if we want to authenticate against a specific database.
193-
$auth_database = isset($config['database']) && !empty($config['database']) ? $config['database'] : null;
193+
$auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null;
194194
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
195195
}
196196

@@ -199,9 +199,10 @@ protected function getHostDsn(array $config)
199199
* @param string $dsn
200200
* @return string
201201
*/
202-
protected function getDatabaseDsn($dsn)
202+
protected function getDatabaseDsn($dsn, $database)
203203
{
204-
return trim(parse_url($dsn, PHP_URL_PATH), '/');
204+
$dsnDatabase = trim(parse_url($dsn, PHP_URL_PATH), '/');
205+
return $dsnDatabase?:$database;
205206
}
206207

207208
/**

0 commit comments

Comments
 (0)