Skip to content

Commit

Permalink
Made connecting to multiple hosts easier with new DSN generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jul 31, 2013
1 parent a9918fa commit d8f6b83
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
--------

Expand Down
38 changes: 24 additions & 14 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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}";
}

/**
Expand Down
17 changes: 16 additions & 1 deletion tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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));
}

}

0 comments on commit d8f6b83

Please sign in to comment.