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
d25ea54d
Commit
d25ea54d
authored
Oct 31, 2016
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ditch extract, update tests and fixes #1012
parent
fa5b1158
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
31 deletions
+27
-31
Connection.php
src/Jenssegers/Mongodb/Connection.php
+10
-12
Model.php
src/Jenssegers/Mongodb/Eloquent/Model.php
+7
-3
ConnectionTest.php
tests/ConnectionTest.php
+8
-14
QueryBuilderTest.php
tests/QueryBuilderTest.php
+2
-2
No files found.
src/Jenssegers/Mongodb/Connection.php
View file @
d25ea54d
...
@@ -127,7 +127,7 @@ class Connection extends \Illuminate\Database\Connection
...
@@ -127,7 +127,7 @@ class Connection extends \Illuminate\Database\Connection
* @param string $dsn
* @param string $dsn
* @param array $config
* @param array $config
* @param array $options
* @param array $options
* @return
MongoDB
* @return
\MongoDB\Client
*/
*/
protected
function
createConnection
(
$dsn
,
array
$config
,
array
$options
)
protected
function
createConnection
(
$dsn
,
array
$config
,
array
$options
)
{
{
...
@@ -165,27 +165,25 @@ class Connection extends \Illuminate\Database\Connection
...
@@ -165,27 +165,25 @@ class Connection extends \Illuminate\Database\Connection
*/
*/
protected
function
getDsn
(
array
$config
)
protected
function
getDsn
(
array
$config
)
{
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the MongoDB and return them back for use.
extract
(
$config
);
// Check if the user passed a complete dsn to the configuration.
// Check if the user passed a complete dsn to the configuration.
if
(
!
empty
(
$
dsn
))
{
if
(
!
empty
(
$
config
[
'dsn'
]
))
{
return
$
dsn
;
return
$
config
[
'dsn'
]
;
}
}
// Treat host option as array of hosts
// Treat host option as array of hosts
$hosts
=
is_array
(
$
host
)
?
$host
:
[
$host
];
$hosts
=
is_array
(
$
config
[
'host'
])
?
$config
[
'host'
]
:
[
$config
[
'host'
]
];
foreach
(
$hosts
as
&
$host
)
{
foreach
(
$hosts
as
&
$host
)
{
// Check if we need to add a port to the host
// Check if we need to add a port to the host
if
(
strpos
(
$host
,
':'
)
===
false
and
isset
(
$port
))
{
if
(
strpos
(
$host
,
':'
)
===
false
&&
!
empty
(
$config
[
'port'
]
))
{
$host
=
"
{
$host
}
:
{
$port
}
"
;
$host
=
$host
.
':'
.
$config
[
'port'
]
;
}
}
}
}
return
"mongodb://"
.
implode
(
','
,
$hosts
)
.
(
$database
?
"/
{
$database
}
"
:
''
);
// Check if we want to authenticate against a specific database.
$auth_database
=
isset
(
$config
[
'options'
])
&&
!
empty
(
$config
[
'options'
][
'database'
])
?
$config
[
'options'
][
'database'
]
:
null
;
return
'mongodb://'
.
implode
(
','
,
$hosts
)
.
(
$auth_database
?
'/'
.
$auth_database
:
''
);
}
}
/**
/**
...
...
src/Jenssegers/Mongodb/Eloquent/Model.php
View file @
d25ea54d
...
@@ -212,13 +212,17 @@ abstract class Model extends BaseModel
...
@@ -212,13 +212,17 @@ abstract class Model extends BaseModel
*/
*/
public
function
getAttribute
(
$key
)
public
function
getAttribute
(
$key
)
{
{
// Check if the key is an array dot notation.
if
(
!
$key
)
{
if
(
$key
and
str_contains
(
$key
,
'.'
)
and
array_has
(
$this
->
attributes
,
$key
))
{
return
;
}
// Dot notation support.
if
(
str_contains
(
$key
,
'.'
)
and
array_has
(
$this
->
attributes
,
$key
))
{
return
$this
->
getAttributeValue
(
$key
);
return
$this
->
getAttributeValue
(
$key
);
}
}
// This checks for embedded relation support.
// This checks for embedded relation support.
if
(
method_exists
(
$this
,
$key
)
&&
!
method_exists
(
self
::
class
,
$key
))
{
if
(
method_exists
(
$this
,
$key
)
and
!
method_exists
(
self
::
class
,
$key
))
{
return
$this
->
getRelationValue
(
$key
);
return
$this
->
getRelationValue
(
$key
);
}
}
...
...
tests/ConnectionTest.php
View file @
d25ea54d
...
@@ -100,33 +100,27 @@ class ConnectionTest extends TestCase
...
@@ -100,33 +100,27 @@ class ConnectionTest extends TestCase
{
{
Config
::
set
(
'database.connections.mongodb.username'
,
'foo'
);
Config
::
set
(
'database.connections.mongodb.username'
,
'foo'
);
Config
::
set
(
'database.connections.mongodb.password'
,
'bar'
);
Config
::
set
(
'database.connections.mongodb.password'
,
'bar'
);
$host
=
Config
::
get
(
'database.connections.mongodb.host'
);
Config
::
set
(
'database.connections.mongodb.options.database'
,
'custom'
);
$port
=
Config
::
get
(
'database.connections.mongodb.port'
,
27017
);
$database
=
Config
::
get
(
'database.connections.mongodb.database'
);
// $this->setExpectedExceptionRegExp('MongoConnectionException', "/Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fail/");
$connection
=
DB
::
connection
(
'mongodb'
);
$connection
=
DB
::
connection
(
'mongodb'
);
$this
->
assertEquals
(
'mongodb://127.0.0.1/custom'
,
(
string
)
$connection
->
getMongoClient
());
}
}
public
function
testCustomPort
()
public
function
testCustom
HostAnd
Port
()
{
{
$port
=
27000
;
Config
::
set
(
'database.connections.mongodb.host'
,
'db1'
);
Config
::
set
(
'database.connections.mongodb.port'
,
$port
);
Config
::
set
(
'database.connections.mongodb.port'
,
27000
);
$host
=
Config
::
get
(
'database.connections.mongodb.host'
);
$database
=
Config
::
get
(
'database.connections.mongodb.database'
);
// $this->setExpectedException('MongoConnectionException', "Failed to connect to: $host:$port: Connection refused");
$connection
=
DB
::
connection
(
'mongodb'
);
$connection
=
DB
::
connection
(
'mongodb'
);
$this
->
assertEquals
(
"mongodb://db1:27000"
,
(
string
)
$connection
->
getMongoClient
());
}
}
public
function
testHostWithPorts
()
public
function
testHostWithPorts
()
{
{
$hosts
=
[
'localhost:27001'
,
'localhost:27002'
];
Config
::
set
(
'database.connections.mongodb.port'
,
27000
);
Config
::
set
(
'database.connections.mongodb.port'
,
27000
);
Config
::
set
(
'database.connections.mongodb.host'
,
[
'localhost:27001'
,
'localhost:27002'
]);
Config
::
set
(
'database.connections.mongodb.host'
,
[
'db1:27001'
,
'db2:27002'
,
'db3:27000'
]);
$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'
);
$connection
=
DB
::
connection
(
'mongodb'
);
$this
->
assertEquals
(
'mongodb://db1:27001,db2:27002,db3:27000'
,
(
string
)
$connection
->
getMongoClient
());
}
}
}
}
tests/QueryBuilderTest.php
View file @
d25ea54d
...
@@ -422,8 +422,8 @@ class QueryBuilderTest extends TestCase
...
@@ -422,8 +422,8 @@ class QueryBuilderTest extends TestCase
public
function
testSubdocumentArrayAggregate
()
public
function
testSubdocumentArrayAggregate
()
{
{
DB
::
collection
(
'items'
)
->
insert
([
DB
::
collection
(
'items'
)
->
insert
([
[
'name'
=>
'knife'
,
'amount'
=>
[[
'hidden'
=>
10
,
'found'
=>
3
],[
'hidden'
=>
5
,
'found'
=>
2
]]],
[
'name'
=>
'knife'
,
'amount'
=>
[[
'hidden'
=>
10
,
'found'
=>
3
],
[
'hidden'
=>
5
,
'found'
=>
2
]]],
[
'name'
=>
'fork'
,
'amount'
=>
[[
'hidden'
=>
35
,
'found'
=>
12
],
[
'hidden'
=>
7
,
'found'
=>
17
],
[
'hidden'
=>
1
,
'found'
=>
19
]]],
[
'name'
=>
'fork'
,
'amount'
=>
[[
'hidden'
=>
35
,
'found'
=>
12
],
[
'hidden'
=>
7
,
'found'
=>
17
],
[
'hidden'
=>
1
,
'found'
=>
19
]]],
[
'name'
=>
'spoon'
,
'amount'
=>
[[
'hidden'
=>
14
,
'found'
=>
21
]]],
[
'name'
=>
'spoon'
,
'amount'
=>
[[
'hidden'
=>
14
,
'found'
=>
21
]]],
[
'name'
=>
'teaspoon'
,
'amount'
=>
[]],
[
'name'
=>
'teaspoon'
,
'amount'
=>
[]],
]);
]);
...
...
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