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
162869b3
Commit
162869b3
authored
Jun 08, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert manual _id's to MongoId objects, fixes #225
parent
23e21f33
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
15 deletions
+33
-15
Model.php
src/Jenssegers/Mongodb/Model.php
+17
-15
ModelTest.php
tests/ModelTest.php
+16
-0
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
162869b3
...
...
@@ -42,11 +42,13 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
public
function
getIdAttribute
(
$value
)
{
// If there is an actual id attribute, then return that.
if
(
$value
)
return
$value
;
if
(
$value
)
return
(
string
)
$value
;
// Return primary key value if present
if
(
array_key_exists
(
$this
->
getKeyName
(),
$this
->
attributes
))
return
$this
->
attributes
[
$this
->
getKeyName
()];
// Return _id as string
if
(
array_key_exists
(
'_id'
,
$this
->
attributes
))
{
return
(
string
)
$this
->
attributes
[
'_id'
];
}
}
/**
...
...
@@ -194,23 +196,23 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
}
/**
*
Get an attribute from
the model.
*
Set a given attribute on
the model.
*
* @param string $key
* @return mixed
* @param mixed $value
* @return void
*/
public
function
getAttribute
(
$key
)
public
function
setAttribute
(
$key
,
$value
)
{
$attribute
=
parent
::
getAttribute
(
$key
);
// If the attribute is a MongoId object, return it as a string.
// This is makes Eloquent relations a lot easier.
if
(
$attribute
instanceof
MongoId
)
// Convert _id to MongoId.
if
(
$key
==
'_id'
and
is_string
(
$value
))
{
return
(
string
)
$attribute
;
$this
->
attributes
[
$key
]
=
new
MongoId
(
$value
);
}
else
{
parent
::
setAttribute
(
$key
,
$value
);
}
return
$attribute
;
}
/**
...
...
tests/ModelTest.php
View file @
162869b3
...
...
@@ -79,6 +79,22 @@ class ModelTest extends TestCase {
$this
->
assertEquals
(
20
,
$check
->
age
);
}
public
function
testManualId
()
{
$user
=
new
User
;
$user
->
_id
=
'4af9f23d8ead0e1d32000000'
;
$user
->
name
=
'John Doe'
;
$user
->
title
=
'admin'
;
$user
->
age
=
35
;
$user
->
save
();
$this
->
assertEquals
(
true
,
$user
->
exists
);
$this
->
assertEquals
(
'4af9f23d8ead0e1d32000000'
,
$user
->
_id
);
$raw
=
$user
->
getAttributes
();
$this
->
assertInstanceOf
(
'MongoId'
,
$raw
[
'_id'
]);
}
public
function
testDelete
()
{
$user
=
new
User
;
...
...
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