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
fe90f122
Commit
fe90f122
authored
Nov 27, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for detach
parent
d709d4a8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
10 deletions
+25
-10
BelongsToMany.php
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+2
-2
RelationsTest.php
tests/RelationsTest.php
+23
-8
No files found.
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
View file @
fe90f122
...
...
@@ -144,7 +144,7 @@ class BelongsToMany extends EloquentBelongsToMany {
*/
protected
function
createAttachRecords
(
$ids
,
array
$attributes
)
{
$records
=
array
();
;
$records
=
array
();
// To create the attachment records, we will simply spin through the IDs given
// and create a new record to insert for each ID. Each ID may actually be a
...
...
@@ -185,7 +185,7 @@ class BelongsToMany extends EloquentBelongsToMany {
// Once we have all of the conditions set on the statement, we are ready
// to run the delete on the pivot table. Then, if the touch parameter
// is true, we will go ahead and touch all related models to sync.
foreach
(
$ids
as
$id
)
foreach
(
$ids
as
$id
)
{
$query
->
pull
(
$this
->
otherKey
,
$id
);
}
...
...
tests/RelationsTest.php
View file @
fe90f122
...
...
@@ -134,7 +134,6 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$user
->
clients
()
->
create
(
array
(
'name'
=>
'Buffet Bar Inc.'
));
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
$client
=
Client
::
with
(
'users'
)
->
first
();
$clients
=
$client
->
getRelation
(
'users'
);
...
...
@@ -148,15 +147,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertCount
(
1
,
$client
->
users
);
// Now create a new user to an existing client
$client
->
users
()
->
create
(
array
(
'name'
=>
'Jane Doe'
));
$otherClient
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
()
->
clients
()
->
get
();
$user
=
$client
->
users
()
->
create
(
array
(
'name'
=>
'Jane Doe'
));
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$
otherClient
);
$this
->
assertInstanceOf
(
'Client'
,
$
otherClient
[
0
]
);
$this
->
assertCount
(
1
,
$
otherClient
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$
user
->
clients
);
$this
->
assertInstanceOf
(
'Client'
,
$
user
->
clients
->
first
()
);
$this
->
assertCount
(
1
,
$
user
->
clients
);
//
Now attach an existing client to an existing user
//
Get user and unattached client
$user
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
();
$client
=
Client
::
Where
(
'name'
,
'='
,
'Buffet Bar Inc.'
)
->
first
();
...
...
@@ -167,6 +164,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are not attached
$this
->
assertFalse
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertFalse
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
$this
->
assertCount
(
1
,
$user
->
clients
);
$this
->
assertCount
(
1
,
$client
->
users
);
// Attach the client to the user
$user
->
clients
()
->
attach
(
$client
);
...
...
@@ -178,6 +177,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are attached
$this
->
assertTrue
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertTrue
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
$this
->
assertCount
(
2
,
$user
->
clients
);
$this
->
assertCount
(
2
,
$client
->
users
);
// Detach clients from user
$user
->
clients
()
->
sync
(
array
());
// Get the new user model
$user
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
();
$client
=
Client
::
Where
(
'name'
,
'='
,
'Buffet Bar Inc.'
)
->
first
();
// Assert they are not attached
$this
->
assertFalse
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertFalse
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
$this
->
assertCount
(
0
,
$user
->
clients
);
$this
->
assertCount
(
1
,
$client
->
users
);
}
public
function
testHasManyAndBelongsToAttachesExistingModels
()
...
...
@@ -205,6 +219,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert there are two client objects in the relationship
$this
->
assertCount
(
2
,
$user
->
clients
);
// Add more clients
$user
->
clients
()
->
sync
(
$moreClients
);
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
...
...
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