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
00b31977
Commit
00b31977
authored
Apr 08, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:jenssegers/Laravel-MongoDB
parents
c2818dc8
b0a8e0d0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
3 deletions
+23
-3
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+18
-2
RelationsTest.php
tests/RelationsTest.php
+5
-1
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
00b31977
...
...
@@ -166,7 +166,7 @@ class EmbedsMany extends Relation {
$this
->
updateTimestamps
(
$model
);
// Insert a new document.
if
(
!
$
model
->
exists
)
if
(
!
$
this
->
contains
(
$model
)
)
{
$result
=
$this
->
performInsert
(
$model
);
}
...
...
@@ -196,6 +196,22 @@ class EmbedsMany extends Relation {
return
count
(
$this
->
getEmbeddedRecords
());
}
/**
* Indicate if a model is already contained in the embedded documents
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
public
function
contains
(
Model
$model
)
{
foreach
(
$this
->
getEmbeddedRecords
()
as
$record
)
{
if
(
$record
[
$model
->
getKeyName
()]
==
$model
->
getKey
())
return
true
;
}
return
false
;
}
/**
* Attach a model instance to the parent model without persistence.
*
...
...
@@ -205,7 +221,7 @@ class EmbedsMany extends Relation {
public
function
associate
(
Model
$model
)
{
// Insert the related model in the parent instance
if
(
!
$
model
->
exists
)
if
(
!
$
this
->
contains
(
$model
)
)
{
return
$this
->
associateNew
(
$model
);
}
...
...
tests/RelationsTest.php
View file @
00b31977
...
...
@@ -399,6 +399,10 @@ class RelationsTest extends TestCase {
$user
->
addresses
()
->
save
(
$address
);
$this
->
assertEquals
(
1
,
$user
->
addresses
->
count
());
$this
->
assertEquals
(
array
(
'Paris'
),
$user
->
addresses
->
lists
(
'city'
));
$user
->
addresses
()
->
create
(
array
(
'_id'
=>
$address
->
_id
,
'city'
=>
'Bruxelles'
));
$this
->
assertEquals
(
1
,
$user
->
addresses
->
count
());
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
}
public
function
testEmbedsManyCreate
()
...
...
@@ -521,7 +525,7 @@ class RelationsTest extends TestCase {
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$address
=
new
Address
(
array
(
'city'
=>
'New York'
));
$
address
->
exists
=
true
;
$
user
->
addresses
()
->
save
(
$address
)
;
$address
->
setEventDispatcher
(
$events
=
Mockery
::
mock
(
'Illuminate\Events\Dispatcher'
));
$events
->
shouldReceive
(
'until'
)
->
once
()
->
with
(
'eloquent.saving: '
.
get_class
(
$address
),
$address
)
->
andReturn
(
true
);
...
...
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