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
aabf95c1
Commit
aabf95c1
authored
Aug 02, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow embedded documents to be saved without leading _
parent
27b52063
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
43 deletions
+36
-43
Model.php
src/Jenssegers/Mongodb/Model.php
+29
-36
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+7
-7
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
aabf95c1
...
...
@@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Collection;
use
Jenssegers\Mongodb\DatabaseManager
as
Resolver
;
use
Jenssegers\Mongodb\Eloquent\Builder
;
use
Jenssegers\Mongodb\Query\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Relations\EmbedsOneOrMany
;
use
Jenssegers\Mongodb\Relations\EmbedsMany
;
use
Jenssegers\Mongodb\Relations\EmbedsOne
;
...
...
@@ -28,13 +29,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
protected
$primaryKey
=
'_id'
;
/**
* The attributes that should be exposed for toArray and toJson.
*
* @var array
*/
protected
$exposed
=
array
();
/**
* The connection resolver instance.
*
...
...
@@ -79,7 +73,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
if
(
is_null
(
$localKey
))
{
$localKey
=
'_'
.
$relation
;
$localKey
=
$relation
;
}
if
(
is_null
(
$foreignKey
))
...
...
@@ -115,7 +109,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
if
(
is_null
(
$localKey
))
{
$localKey
=
'_'
.
$relation
;
$localKey
=
$relation
;
}
if
(
is_null
(
$foreignKey
))
...
...
@@ -221,6 +215,32 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
}
}
$camelKey
=
camel_case
(
$key
);
// If the "attribute" exists as a method on the model, it may be an
// embedded model. If so, we need to return the result before it
// is handled by the parent method.
if
(
method_exists
(
$this
,
$camelKey
))
{
$relations
=
$this
->
$camelKey
();
// This attribute matches an embedsOne or embedsMany relation so we need
// to return the relation results instead of the interal attributes.
if
(
$relations
instanceof
EmbedsOneOrMany
)
{
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if
(
array_key_exists
(
$key
,
$this
->
relations
))
{
return
$this
->
relations
[
$key
];
}
// Get the relation results.
return
$this
->
getRelationshipFromMethod
(
$key
,
$camelKey
);
}
}
return
parent
::
getAttribute
(
$key
);
}
...
...
@@ -286,22 +306,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
{
$value
=
(
string
)
$value
;
}
// If the attribute starts with an underscore, it might be the
// internal array of embedded documents. In that case, we need
// to hide these from the output so that the relation-based
// attribute can take over.
else
if
(
starts_with
(
$key
,
'_'
)
and
!
in_array
(
$key
,
$this
->
exposed
))
{
$camelKey
=
camel_case
(
$key
);
// If we can find a method that responds to this relation we
// will remove it from the output.
if
(
method_exists
(
$this
,
$camelKey
))
{
unset
(
$attributes
[
$key
]);
}
}
}
return
$attributes
;
...
...
@@ -356,17 +360,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return
call_user_func_array
(
array
(
$query
,
'pull'
),
func_get_args
());
}
/**
* Set the exposed attributes for the model.
*
* @param array $exposed
* @return void
*/
public
function
setExposed
(
array
$exposed
)
{
$this
->
exposed
=
$exposed
;
}
/**
* Create a new Eloquent query builder for the model.
*
...
...
tests/EmbeddedRelationsTest.php
View file @
aabf95c1
...
...
@@ -29,7 +29,8 @@ class EmbeddedRelationsTest extends TestCase {
$address
=
$user
->
addresses
()
->
save
(
$address
);
$address
->
unsetEventDispatcher
();
$this
->
assertNotNull
(
$user
->
_addresses
);
$this
->
assertNotNull
(
$user
->
addresses
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$user
->
addresses
);
$this
->
assertEquals
(
array
(
'London'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
created_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
updated_at
);
...
...
@@ -328,13 +329,15 @@ class EmbeddedRelationsTest extends TestCase {
$user
=
User
::
find
(
$user1
->
id
);
$relations
=
$user
->
getRelations
();
$this
->
assertFalse
(
array_key_exists
(
'addresses'
,
$relations
));
$this
->
assertArrayNotHasKey
(
'addresses'
,
$user
->
toArray
());
$this
->
assertArrayHasKey
(
'addresses'
,
$user
->
toArray
());
$this
->
assertTrue
(
is_array
(
$user
->
toArray
()[
'addresses'
]));
$user
=
User
::
with
(
'addresses'
)
->
get
()
->
first
();
$relations
=
$user
->
getRelations
();
$this
->
assertTrue
(
array_key_exists
(
'addresses'
,
$relations
));
$this
->
assertEquals
(
2
,
$relations
[
'addresses'
]
->
count
());
$this
->
assertArrayHasKey
(
'addresses'
,
$user
->
toArray
());
$this
->
assertTrue
(
is_array
(
$user
->
toArray
()[
'addresses'
]));
}
public
function
testEmbedsManyDelete
()
...
...
@@ -459,11 +462,8 @@ class EmbeddedRelationsTest extends TestCase {
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Brussels'
)));
$array
=
$user
->
toArray
();
$this
->
assertArrayNotHasKey
(
'_addresses'
,
$array
);
$user
->
setExposed
(
array
(
'_addresses'
));
$array
=
$user
->
toArray
();
$this
->
assertArrayHasKey
(
'_addresses'
,
$array
);
$this
->
assertArrayHasKey
(
'addresses'
,
$array
);
$this
->
assertTrue
(
is_array
(
$array
[
'addresses'
]));
}
}
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