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
d2c1da3a
Commit
d2c1da3a
authored
Aug 08, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into embedded-models
parents
e1750a63
7534e6f9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
7 deletions
+56
-7
Connection.php
src/Jenssegers/Mongodb/Connection.php
+10
-0
Model.php
src/Jenssegers/Mongodb/Model.php
+14
-5
ConnectionTest.php
tests/ConnectionTest.php
+2
-1
ModelTest.php
tests/ModelTest.php
+30
-1
No files found.
src/Jenssegers/Mongodb/Connection.php
View file @
d2c1da3a
...
...
@@ -133,6 +133,16 @@ class Connection extends \Illuminate\Database\Connection {
return
new
MongoClient
(
$dsn
,
$options
);
}
/**
* Disconnect from the underlying MongoClient connection.
*
* @return void
*/
public
function
disconnect
()
{
$this
->
connection
->
close
();
}
/**
* Create a DSN string from a configuration.
*
...
...
src/Jenssegers/Mongodb/Model.php
View file @
d2c1da3a
...
...
@@ -39,17 +39,26 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
/**
* Custom accessor for the model's id.
*
* @return string
* @param mixed $value
*
* @return mixed
*/
public
function
getIdAttribute
(
$value
)
{
if
(
$value
)
return
(
string
)
$value
;
// If we don't have a value for 'id', we will use the Mongo '_id' value.
// This allows us to work with models in a more sql-like way.
if
(
!
$value
and
array_key_exists
(
'_id'
,
$this
->
attributes
))
{
$value
=
$this
->
attributes
[
'_id'
];
}
//
Return _id as string
if
(
array_key_exists
(
'_id'
,
$this
->
attributes
)
)
//
Convert MongoId's to string.
if
(
$value
instanceof
MongoId
)
{
return
(
string
)
$
this
->
attributes
[
'_id'
]
;
return
(
string
)
$
value
;
}
return
$value
;
}
/**
...
...
tests/ConnectionTest.php
View file @
d2c1da3a
...
...
@@ -15,7 +15,8 @@ class ConnectionTest extends TestCase {
$this
->
assertEquals
(
spl_object_hash
(
$c1
),
spl_object_hash
(
$c2
));
$c1
=
DB
::
connection
(
'mongodb'
);
$c2
=
DB
::
reconnect
(
'mongodb'
);
DB
::
purge
(
'mongodb'
);
$c2
=
DB
::
connection
(
'mongodb'
);
$this
->
assertNotEquals
(
spl_object_hash
(
$c1
),
spl_object_hash
(
$c2
));
}
...
...
tests/ModelTest.php
View file @
d2c1da3a
...
...
@@ -79,7 +79,7 @@ class ModelTest extends TestCase {
$this
->
assertEquals
(
20
,
$check
->
age
);
}
public
function
testManualId
()
public
function
testManual
String
Id
()
{
$user
=
new
User
;
$user
->
_id
=
'4af9f23d8ead0e1d32000000'
;
...
...
@@ -93,6 +93,35 @@ class ModelTest extends TestCase {
$raw
=
$user
->
getAttributes
();
$this
->
assertInstanceOf
(
'MongoId'
,
$raw
[
'_id'
]);
$user
=
new
User
;
$user
->
_id
=
'customId'
;
$user
->
name
=
'John Doe'
;
$user
->
title
=
'admin'
;
$user
->
age
=
35
;
$user
->
save
();
$this
->
assertEquals
(
true
,
$user
->
exists
);
$this
->
assertEquals
(
'customId'
,
$user
->
_id
);
$raw
=
$user
->
getAttributes
();
$this
->
assertInternalType
(
'string'
,
$raw
[
'_id'
]);
}
public
function
testManualIntId
()
{
$user
=
new
User
;
$user
->
_id
=
1
;
$user
->
name
=
'John Doe'
;
$user
->
title
=
'admin'
;
$user
->
age
=
35
;
$user
->
save
();
$this
->
assertEquals
(
true
,
$user
->
exists
);
$this
->
assertEquals
(
1
,
$user
->
_id
);
$raw
=
$user
->
getAttributes
();
$this
->
assertInternalType
(
'integer'
,
$raw
[
'_id'
]);
}
public
function
testDelete
()
...
...
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