Commit d8f6b839 authored by Jens Segers's avatar Jens Segers

Made connecting to multiple hosts easier with new DSN generation

parent a9918fa7
...@@ -36,7 +36,9 @@ This package will automatically check the database configuration in `app/config/ ...@@ -36,7 +36,9 @@ This package will automatically check the database configuration in `app/config/
'mongodb' => array( 'mongodb' => array(
'host' => 'localhost', 'host' => 'localhost',
'port' => 27017, '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: 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 ...@@ -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 Eloquent
-------- --------
......
...@@ -80,11 +80,21 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -80,11 +80,21 @@ class Connection extends \Illuminate\Database\Connection {
* *
* @return MongoDB * @return MongoDB
*/ */
public function getDb() public function getMongoDB()
{ {
return $this->db; return $this->db;
} }
/**
* return MongoClient object
*
* @return MongoClient
*/
public function getMongoClient()
{
return $this->connection;
}
/** /**
* Create a DSN string from a configuration. * Create a DSN string from a configuration.
* *
...@@ -98,23 +108,23 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -98,23 +108,23 @@ class Connection extends \Illuminate\Database\Connection {
// need to establish the MongoClient and return them back for use. // need to establish the MongoClient and return them back for use.
extract($config); 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}"; return "mongodb://" . implode(',', $hosts) . "/{$database}";
if (isset($config['port']))
{
$dsn .= ":{$port}";
}
$dsn .= "/{$database}";
return $dsn;
} }
/** /**
......
...@@ -28,7 +28,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -28,7 +28,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
public function testDb() public function testDb()
{ {
$connection = DB::connection('mongodb'); $connection = DB::connection('mongodb');
$this->assertInstanceOf('MongoDB', $connection->getDb()); $this->assertInstanceOf('MongoDB', $connection->getMongoDB());
} }
public function testCollection() public function testCollection()
...@@ -49,4 +49,19 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -49,4 +49,19 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(is_array($dbs)); $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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment