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
a86d16a3
Commit
a86d16a3
authored
Mar 08, 2014
by
Alexandre Butynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add method dissociate to EmbedsMany and refactor destroy
parent
cbb5273d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
16 deletions
+57
-16
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+39
-14
RelationsTest.php
tests/RelationsTest.php
+18
-2
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
a86d16a3
...
...
@@ -273,22 +273,12 @@ class EmbedsMany extends Relation {
/**
* Destroy the embedded models for the given IDs.
*
* @param
array|int
$ids
* @param
mixed
$ids
* @return int
*/
public
function
destroy
(
$ids
=
array
())
{
// We'll initialize a count here so we will return the total number of deletes
// for the operation. The developers can then check this number as a boolean
// type value or get this total count of records deleted for logging, etc.
$count
=
0
;
if
(
$ids
instanceof
Model
)
$ids
=
(
array
)
$ids
->
getKey
();
// If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
$ids
=
(
array
)
$ids
;
$ids
=
$this
->
getIdsArrayFrom
(
$ids
);
$primaryKey
=
$this
->
related
->
getKeyName
();
...
...
@@ -296,9 +286,23 @@ class EmbedsMany extends Relation {
foreach
(
$ids
as
$id
)
{
$this
->
query
->
pull
(
$this
->
localKey
,
array
(
$primaryKey
=>
$this
->
getForeignKeyValue
(
$id
)));
$count
++
;
}
return
$this
->
dissociate
(
$ids
);
}
/**
* Dissociate the embedded models for the given IDs without persistence.
*
* @param mixed $ids
* @return int
*/
public
function
dissociate
(
$ids
=
array
())
{
$ids
=
$this
->
getIdsArrayFrom
(
$ids
);
$primaryKey
=
$this
->
related
->
getKeyName
();
// Get existing embedded documents.
$documents
=
$this
->
getEmbeddedRecords
();
...
...
@@ -313,7 +317,28 @@ class EmbedsMany extends Relation {
$this
->
setEmbeddedRecords
(
$documents
);
return
$count
;
// We return the total number of deletes for the operation. The developers
// can then check this number as a boolean type value or get this total count
// of records deleted for logging, etc.
return
count
(
$ids
);
}
/**
* Transform single ID, single Model or array of Models into an array of IDs
*
* @param mixed $ids
* @return int
*/
protected
function
getIdsArrayFrom
(
$ids
)
{
if
(
!
is_array
(
$ids
))
$ids
=
array
(
$ids
);
foreach
(
$ids
as
&
$id
)
{
if
(
$id
instanceof
Model
)
$id
=
$id
->
getKey
();
}
return
$ids
;
}
/**
...
...
tests/RelationsTest.php
View file @
a86d16a3
...
...
@@ -383,8 +383,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Paris'
));
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'San Francisco'
));
$
u
ser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(
'Bruxelles'
,
'Paris'
,
'San Francisco'
),
$
u
ser
->
addresses
->
lists
(
'city'
));
$
freshU
ser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(
'Bruxelles'
,
'Paris'
,
'San Francisco'
),
$
freshU
ser
->
addresses
->
lists
(
'city'
));
$ids
=
$user
->
addresses
->
lists
(
'_id'
);
$user
->
addresses
()
->
destroy
(
$ids
);
...
...
@@ -392,6 +392,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$freshUser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(),
$freshUser
->
addresses
->
lists
(
'city'
));
list
(
$london
,
$bristol
,
$bruxelles
)
=
$user
->
addresses
()
->
saveMany
(
array
(
new
Address
(
array
(
'city'
=>
'London'
)),
new
Address
(
array
(
'city'
=>
'Bristol'
)),
new
Address
(
array
(
'city'
=>
'Bruxelles'
))));
$user
->
addresses
()
->
destroy
(
array
(
$london
,
$bruxelles
));
$this
->
assertEquals
(
array
(
'Bristol'
),
$user
->
addresses
->
lists
(
'city'
));
}
public
function
testEmbedsManyDissociate
()
{
$user
=
User
::
create
(
array
());
$cordoba
=
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Cordoba'
));
$user
->
addresses
()
->
dissociate
(
$cordoba
->
id
);
$freshUser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
0
,
$user
->
addresses
->
count
());
$this
->
assertEquals
(
1
,
$freshUser
->
addresses
->
count
());
}
public
function
testEmbedsManyAliases
()
...
...
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