From d8f6b839673db276d086233bc4f55fe7bcfab9d7 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 1 Aug 2013 00:17:14 +0200 Subject: [PATCH] Made connecting to multiple hosts easier with new DSN generation --- README.md | 15 ++++++++++- src/Jenssegers/Mongodb/Connection.php | 38 +++++++++++++++++---------- tests/ConnectionTest.php | 17 +++++++++++- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 112832297..19d0c151b 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,9 @@ This package will automatically check the database configuration in `app/config/ 'mongodb' => array( 'host' => 'localhost', 'port' => 27017, - 'database' => 'database', + 'username' => 'username', + 'password' => 'password', + 'database' => 'database' ), You can also specify the connection name in the model if you have multiple connections: @@ -47,6 +49,17 @@ You can also specify the connection name in the model if you have multiple conne } +You can connect to multiple servers or replica sets with the following configuration: + + 'mongodb' => array( + 'host' => array('server1', 'server2), + 'port' => 27017, + 'username' => 'username', + 'password' => 'password', + 'database' => 'database', + 'options' => array('replicaSet' => 'replicaSetName') + ), + Eloquent -------- diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 07108b92a..733d2bbdf 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -80,11 +80,21 @@ public function getCollection($name) * * @return MongoDB */ - public function getDb() + public function getMongoDB() { return $this->db; } + /** + * return MongoClient object + * + * @return MongoClient + */ + public function getMongoClient() + { + return $this->connection; + } + /** * Create a DSN string from a configuration. * @@ -98,23 +108,23 @@ protected function getDsn(array $config) // need to establish the MongoClient and return them back for use. extract($config); - $dsn = "mongodb://"; + // Treat host option as array of hosts + $hosts = is_array($config['host']) ? $config['host'] : array($config['host']); - if (isset($config['username']) and isset($config['password'])) + foreach ($hosts as &$host) { - $dsn .= "{$username}:{$password}@"; + if (isset($config['username']) and isset($config['password'])) + { + $host = "{$username}:{$password}@{$host}"; + } + + if (isset($config['port'])) + { + $host = "{$host}:{$port}"; + } } - $dsn .= "{$host}"; - - if (isset($config['port'])) - { - $dsn .= ":{$port}"; - } - - $dsn .= "/{$database}"; - - return $dsn; + return "mongodb://" . implode(',', $hosts) . "/{$database}"; } /** diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index bfef39054..f02033f00 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -28,7 +28,7 @@ public function testConnection() public function testDb() { $connection = DB::connection('mongodb'); - $this->assertInstanceOf('MongoDB', $connection->getDb()); + $this->assertInstanceOf('MongoDB', $connection->getMongoDB()); } public function testCollection() @@ -49,4 +49,19 @@ public function testDynamic() $this->assertTrue(is_array($dbs)); } + public function testMultipleConnections() + { + global $app; + + # Add fake host + $db = $app['config']['database.connections']['mongodb']; + $db['host'] = array($db['host'], '1.2.3.4'); + + $connection = new Connection($db); + $mongoclient = $connection->getMongoClient(); + + $hosts = $mongoclient->getHosts(); + $this->assertEquals(1, count($hosts)); + } + } \ No newline at end of file