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
84d8fd5e
Commit
84d8fd5e
authored
Mar 22, 2015
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #439
parent
a4712ac1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
12 deletions
+37
-12
BelongsToMany.php
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+9
-12
RelationsTest.php
tests/RelationsTest.php
+28
-0
No files found.
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
View file @
84d8fd5e
...
...
@@ -104,7 +104,7 @@ class BelongsToMany extends EloquentBelongsToMany {
$records
=
$this
->
formatSyncList
(
$ids
);
$detach
=
array_
diff
(
$current
,
array_keys
(
$records
));
$detach
=
array_
values
(
array_diff
(
$current
,
array_keys
(
$records
)
));
// Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the
...
...
@@ -159,31 +159,28 @@ class BelongsToMany extends EloquentBelongsToMany {
$model
=
$id
;
$id
=
$model
->
getKey
();
}
$records
=
$this
->
createAttachRecords
((
array
)
$id
,
$attributes
);
// Get the ids to attach to the parent and related model.
$otherIds
=
array_pluck
(
$records
,
$this
->
otherKey
);
$foreignIds
=
array_pluck
(
$records
,
$this
->
foreignKey
);
$ids
=
(
array
)
$id
;
// Attach the new ids to the parent model.
$this
->
parent
->
push
(
$this
->
otherKey
,
$
otherI
ds
,
true
);
$this
->
parent
->
push
(
$this
->
otherKey
,
$
i
ds
,
true
);
// If we have a model instance, we can p
su
h the ids to that model,
// If we have a model instance, we can p
us
h the ids to that model,
// so that the internal attributes are updated as well. Otherwise,
// we will just perform a regular database query.
if
(
isset
(
$model
))
{
// Attach the new ids to the related model.
$model
->
push
(
$this
->
foreignKey
,
$
foreignIds
,
true
);
$model
->
push
(
$this
->
foreignKey
,
$
this
->
parent
->
getKey
()
,
true
);
}
else
{
$query
=
$this
->
newRelatedQuery
();
$query
->
where
(
$this
->
related
->
getKeyName
(),
$id
);
// Select related models.
$query
->
whereIn
(
$this
->
related
->
getKeyName
(),
$ids
);
// Attach the new
ids
to the related model.
$query
->
push
(
$this
->
foreignKey
,
$
foreignIds
,
true
);
// Attach the new
parent id
to the related model.
$query
->
push
(
$this
->
foreignKey
,
$
this
->
parent
->
getKey
()
,
true
);
}
if
(
$touch
)
$this
->
touchIfTouching
();
...
...
tests/RelationsTest.php
View file @
84d8fd5e
...
...
@@ -277,6 +277,34 @@ class RelationsTest extends TestCase {
$this
->
assertCount
(
1
,
$user
->
clients
);
}
public
function
testBelongsToManyAttachArray
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$client1
=
Client
::
create
(
array
(
'name'
=>
'Test 1'
))
->
_id
;
$client2
=
Client
::
create
(
array
(
'name'
=>
'Test 2'
))
->
_id
;
$user
=
User
::
where
(
'name'
,
'='
,
'John Doe'
)
->
first
();
$user
->
clients
()
->
attach
([
$client1
,
$client2
]);
$this
->
assertCount
(
2
,
$user
->
clients
);
}
public
function
testBelongsToManySyncAlreadyPresent
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$client1
=
Client
::
create
(
array
(
'name'
=>
'Test 1'
))
->
_id
;
$client2
=
Client
::
create
(
array
(
'name'
=>
'Test 2'
))
->
_id
;
$user
->
clients
()
->
sync
([
$client1
,
$client2
]);
$this
->
assertCount
(
2
,
$user
->
clients
);
$user
=
User
::
where
(
'name'
,
'='
,
'John Doe'
)
->
first
();
$user
->
clients
()
->
sync
([
$client1
]);
$this
->
assertCount
(
1
,
$user
->
clients
);
$user
=
User
::
where
(
'name'
,
'='
,
'John Doe'
)
->
first
()
->
toArray
();
$this
->
assertCount
(
1
,
$user
[
'client_ids'
]);
}
public
function
testBelongsToManyCustom
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
...
...
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