File tree Expand file tree Collapse file tree 3 files changed +54
-16
lines changed Expand file tree Collapse file tree 3 files changed +54
-16
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,9 @@ This package will automatically check the database configuration in `app/config/
3636 'mongodb' => array(
3737 'host' => 'localhost',
3838 'port' => 27017,
39- 'database' => 'database',
39+ 'username' => 'username',
40+ 'password' => 'password',
41+ 'database' => 'database'
4042 ),
4143
4244You 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
4749
4850 }
4951
52+ You can connect to multiple servers or replica sets with the following configuration:
53+
54+ 'mongodb' => array(
55+ 'host' => array('server1', 'server2),
56+ 'port' => 27017,
57+ 'username' => 'username',
58+ 'password' => 'password',
59+ 'database' => 'database',
60+ 'options' => array('replicaSet' => 'replicaSetName')
61+ ),
62+
5063Eloquent
5164--------
5265
Original file line number Diff line number Diff line change @@ -80,11 +80,21 @@ public function getCollection($name)
8080 *
8181 * @return MongoDB
8282 */
83- public function getDb ()
83+ public function getMongoDB ()
8484 {
8585 return $ this ->db ;
8686 }
8787
88+ /**
89+ * return MongoClient object
90+ *
91+ * @return MongoClient
92+ */
93+ public function getMongoClient ()
94+ {
95+ return $ this ->connection ;
96+ }
97+
8898 /**
8999 * Create a DSN string from a configuration.
90100 *
@@ -98,23 +108,23 @@ protected function getDsn(array $config)
98108 // need to establish the MongoClient and return them back for use.
99109 extract ($ config );
100110
101- $ dsn = "mongodb:// " ;
111+ // Treat host option as array of hosts
112+ $ hosts = is_array ($ config ['host ' ]) ? $ config ['host ' ] : array ($ config ['host ' ]);
102113
103- if ( isset ( $ config [ ' username ' ]) and isset ( $ config [ ' password ' ]) )
114+ foreach ( $ hosts as & $ host )
104115 {
105- $ dsn .= "{$ username }: {$ password }@ " ;
116+ if (isset ($ config ['username ' ]) and isset ($ config ['password ' ]))
117+ {
118+ $ host = "{$ username }: {$ password }@ {$ host }" ;
119+ }
120+
121+ if (isset ($ config ['port ' ]))
122+ {
123+ $ host = "{$ host }: {$ port }" ;
124+ }
106125 }
107126
108- $ dsn .= "{$ host }" ;
109-
110- if (isset ($ config ['port ' ]))
111- {
112- $ dsn .= ": {$ port }" ;
113- }
114-
115- $ dsn .= "/ {$ database }" ;
116-
117- return $ dsn ;
127+ return "mongodb:// " . implode (', ' , $ hosts ) . "/ {$ database }" ;
118128 }
119129
120130 /**
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ public function testConnection()
2828 public function testDb ()
2929 {
3030 $ connection = DB ::connection ('mongodb ' );
31- $ this ->assertInstanceOf ('MongoDB ' , $ connection ->getDb ());
31+ $ this ->assertInstanceOf ('MongoDB ' , $ connection ->getMongoDB ());
3232 }
3333
3434 public function testCollection ()
@@ -49,4 +49,19 @@ public function testDynamic()
4949 $ this ->assertTrue (is_array ($ dbs ));
5050 }
5151
52+ public function testMultipleConnections ()
53+ {
54+ global $ app ;
55+
56+ # Add fake host
57+ $ db = $ app ['config ' ]['database.connections ' ]['mongodb ' ];
58+ $ db ['host ' ] = array ($ db ['host ' ], '1.2.3.4 ' );
59+
60+ $ connection = new Connection ($ db );
61+ $ mongoclient = $ connection ->getMongoClient ();
62+
63+ $ hosts = $ mongoclient ->getHosts ();
64+ $ this ->assertEquals (1 , count ($ hosts ));
65+ }
66+
5267}
You can’t perform that action at this time.
0 commit comments