Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
d8f6b839
Commit
d8f6b839
authored
Jul 31, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made connecting to multiple hosts easier with new DSN generation
parent
a9918fa7
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
16 deletions
+54
-16
README.md
README.md
+14
-1
Connection.php
src/Jenssegers/Mongodb/Connection.php
+24
-14
ConnectionTest.php
tests/ConnectionTest.php
+16
-1
No files found.
README.md
View file @
d8f6b839
...
@@ -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
--------
--------
...
...
src/Jenssegers/Mongodb/Connection.php
View file @
d8f6b839
...
@@ -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
get
Db
()
public
function
get
MongoDB
()
{
{
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'
]);
foreach
(
$hosts
as
&
$host
)
{
if
(
isset
(
$config
[
'username'
])
and
isset
(
$config
[
'password'
]))
if
(
isset
(
$config
[
'username'
])
and
isset
(
$config
[
'password'
]))
{
{
$dsn
.=
"
{
$username
}
:
{
$password
}
@
"
;
$host
=
"
{
$username
}
:
{
$password
}
@
{
$host
}
"
;
}
}
$dsn
.=
"
{
$host
}
"
;
if
(
isset
(
$config
[
'port'
]))
if
(
isset
(
$config
[
'port'
]))
{
{
$dsn
.=
":
{
$port
}
"
;
$host
=
"
{
$host
}
:
{
$port
}
"
;
}
}
}
$dsn
.=
"/
{
$database
}
"
;
return
"mongodb://"
.
implode
(
','
,
$hosts
)
.
"/
{
$database
}
"
;
return
$dsn
;
}
}
/**
/**
...
...
tests/ConnectionTest.php
View file @
d8f6b839
...
@@ -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
->
get
Db
());
$this
->
assertInstanceOf
(
'MongoDB'
,
$connection
->
get
MongoDB
());
}
}
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment