File tree 3 files changed +54
-16
lines changed
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/
36
36
'mongodb' => array(
37
37
'host' => 'localhost',
38
38
'port' => 27017,
39
- 'database' => 'database',
39
+ 'username' => 'username',
40
+ 'password' => 'password',
41
+ 'database' => 'database'
40
42
),
41
43
42
44
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
49
48
50
}
49
51
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
+
50
63
Eloquent
51
64
--------
52
65
Original file line number Diff line number Diff line change @@ -80,11 +80,21 @@ public function getCollection($name)
80
80
*
81
81
* @return MongoDB
82
82
*/
83
- public function getDb ()
83
+ public function getMongoDB ()
84
84
{
85
85
return $ this ->db ;
86
86
}
87
87
88
+ /**
89
+ * return MongoClient object
90
+ *
91
+ * @return MongoClient
92
+ */
93
+ public function getMongoClient ()
94
+ {
95
+ return $ this ->connection ;
96
+ }
97
+
88
98
/**
89
99
* Create a DSN string from a configuration.
90
100
*
@@ -98,23 +108,23 @@ protected function getDsn(array $config)
98
108
// need to establish the MongoClient and return them back for use.
99
109
extract ($ config );
100
110
101
- $ dsn = "mongodb:// " ;
111
+ // Treat host option as array of hosts
112
+ $ hosts = is_array ($ config ['host ' ]) ? $ config ['host ' ] : array ($ config ['host ' ]);
102
113
103
- if ( isset ( $ config [ ' username ' ]) and isset ( $ config [ ' password ' ]) )
114
+ foreach ( $ hosts as & $ host )
104
115
{
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
+ }
106
125
}
107
126
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 }" ;
118
128
}
119
129
120
130
/**
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ public function testConnection()
28
28
public function testDb ()
29
29
{
30
30
$ connection = DB ::connection ('mongodb ' );
31
- $ this ->assertInstanceOf ('MongoDB ' , $ connection ->getDb ());
31
+ $ this ->assertInstanceOf ('MongoDB ' , $ connection ->getMongoDB ());
32
32
}
33
33
34
34
public function testCollection ()
@@ -49,4 +49,19 @@ public function testDynamic()
49
49
$ this ->assertTrue (is_array ($ dbs ));
50
50
}
51
51
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
+
52
67
}
You can’t perform that action at this time.
0 commit comments