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
97dbdfc7
Commit
97dbdfc7
authored
Feb 28, 2015
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Connection tweaks, fixes #413
parent
ca1a9292
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
58 additions
and
26 deletions
+58
-26
Model.php
src/Jenssegers/Eloquent/Model.php
+3
-3
Collection.php
src/Jenssegers/Mongodb/Collection.php
+1
-2
Connection.php
src/Jenssegers/Mongodb/Connection.php
+27
-9
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+8
-12
Processor.php
src/Jenssegers/Mongodb/Query/Processor.php
+8
-0
ConnectionTest.php
tests/ConnectionTest.php
+11
-0
No files found.
src/Jenssegers/Eloquent/Model.php
View file @
97dbdfc7
...
...
@@ -278,7 +278,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
// Check the connection type
if
(
$connection
instanceof
\Jenssegers\Mongodb\Connection
)
{
return
new
QueryBuilder
(
$connection
);
return
new
QueryBuilder
(
$connection
,
$connection
->
getPostProcessor
()
);
}
return
parent
::
newBaseQueryBuilder
();
...
...
src/Jenssegers/Mongodb/Collection.php
View file @
97dbdfc7
<?php
namespace
Jenssegers\Mongodb
;
use
Exception
;
use
MongoCollection
;
use
Exception
,
MongoCollection
;
use
Jenssegers\Mongodb\Connection
;
class
Collection
{
...
...
src/Jenssegers/Mongodb/Connection.php
View file @
97dbdfc7
<?php
namespace
Jenssegers\Mongodb
;
use
Jenssegers\Mongodb\Collection
;
use
Jenssegers\Mongodb\Query\Builder
as
QueryBuilder
;
use
MongoClient
;
class
Connection
extends
\Illuminate\Database\Connection
{
...
...
@@ -34,13 +32,25 @@ class Connection extends \Illuminate\Database\Connection {
$dsn
=
$this
->
getDsn
(
$config
);
// You can pass options directly to the MongoClient constructor
$options
=
array_get
(
$config
,
'options'
,
array
()
);
$options
=
array_get
(
$config
,
'options'
,
[]
);
// Create the connection
$this
->
connection
=
$this
->
createConnection
(
$dsn
,
$config
,
$options
);
// Select database
$this
->
db
=
$this
->
connection
->
{
$config
[
'database'
]};
$this
->
useDefaultPostProcessor
();
}
/**
* Get the default post processor instance.
*
* @return Query\Processor
*/
protected
function
getDefaultPostProcessor
()
{
return
new
Query\Processor
;
}
/**
...
...
@@ -51,7 +61,9 @@ class Connection extends \Illuminate\Database\Connection {
*/
public
function
collection
(
$collection
)
{
$query
=
new
QueryBuilder
(
$this
);
$processor
=
$this
->
getPostProcessor
();
$query
=
new
Query\Builder
(
$this
,
$processor
);
return
$query
->
from
(
$collection
);
}
...
...
@@ -120,12 +132,12 @@ class Connection extends \Illuminate\Database\Connection {
{
// Add credentials as options, this makes sure the connection will not fail if
// the username or password contains strange characters.
if
(
isset
(
$config
[
'username'
])
&&
$config
[
'username'
]
)
if
(
!
empty
(
$config
[
'username'
])
)
{
$options
[
'username'
]
=
$config
[
'username'
];
}
if
(
isset
(
$config
[
'password'
])
&&
$config
[
'password'
]
)
if
(
!
empty
(
$config
[
'password'
])
)
{
$options
[
'password'
]
=
$config
[
'password'
];
}
...
...
@@ -156,13 +168,19 @@ class Connection extends \Illuminate\Database\Connection {
// need to establish the MongoClient and return them back for use.
extract
(
$config
);
// Check if the user passed a complete dsn to the configuration.
if
(
!
empty
(
$dsn
))
{
return
$dsn
;
}
// Treat host option as array of hosts
$hosts
=
is_array
(
$
config
[
'host'
])
?
$config
[
'host'
]
:
array
(
$config
[
'host'
])
;
$hosts
=
is_array
(
$
host
)
?
$host
:
[
$host
]
;
// Add ports to hosts
foreach
(
$hosts
as
&
$host
)
{
if
(
isset
(
$config
[
'port'
]))
// Check if we need to add a port to the host
if
(
strpos
(
$host
,
':'
)
===
false
and
isset
(
$port
))
{
$host
=
"
{
$host
}
:
{
$port
}
"
;
}
...
...
src/Jenssegers/Mongodb/Query/Builder.php
View file @
97dbdfc7
<?php
namespace
Jenssegers\Mongodb\Query
;
use
MongoId
;
use
MongoRegex
;
use
MongoDate
;
use
DateTime
;
use
Closure
;
use
Illuminate\Database\Query\Builder
as
QueryBuilder
;
use
MongoId
,
MongoRegex
,
MongoDate
,
DateTime
,
Closure
;
use
Illuminate\Database\Query\Builder
as
BaseBuilder
;
use
Illuminate\Database\Query\Expression
;
use
Jenssegers\Mongodb\Connection
;
class
Builder
extends
Query
Builder
{
class
Builder
extends
Base
Builder
{
/**
* The database collection
...
...
@@ -69,9 +64,10 @@ class Builder extends QueryBuilder {
* @param Connection $connection
* @return void
*/
public
function
__construct
(
Connection
$connection
)
public
function
__construct
(
Connection
$connection
,
Processor
$processor
)
{
$this
->
connection
=
$connection
;
$this
->
processor
=
$processor
;
}
/**
...
...
@@ -656,7 +652,7 @@ class Builder extends QueryBuilder {
*/
public
function
newQuery
()
{
return
new
Builder
(
$this
->
connection
);
return
new
Builder
(
$this
->
connection
,
$this
->
processor
);
}
/**
...
...
src/Jenssegers/Mongodb/Query/Processor.php
0 → 100644
View file @
97dbdfc7
<?php
namespace
Jenssegers\Mongodb\Query
;
use
Illuminate\Database\Query\Processors\Processor
as
BaseProcessor
;
class
Processor
extends
BaseProcessor
{
}
tests/ConnectionTest.php
View file @
97dbdfc7
...
...
@@ -119,4 +119,15 @@ class ConnectionTest extends TestCase {
$connection
=
DB
::
connection
(
'mongodb'
);
}
public
function
testHostWithPorts
()
{
$hosts
=
[
'localhost:27001'
,
'localhost:27002'
];
Config
::
set
(
'database.connections.mongodb.port'
,
27000
);
Config
::
set
(
'database.connections.mongodb.host'
,
[
'localhost:27001'
,
'localhost:27002'
]);
$database
=
Config
::
get
(
'database.connections.mongodb.database'
);
$this
->
setExpectedException
(
'MongoConnectionException'
,
"Failed to connect to: "
.
$hosts
[
0
]
.
": Connection refused; Failed to connect to: "
.
$hosts
[
1
]
.
": Connection refused"
);
$connection
=
DB
::
connection
(
'mongodb'
);
}
}
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