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
b9e1efe8
Commit
b9e1efe8
authored
Apr 19, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding delete method for embedded documents
parent
418c3dbf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
24 deletions
+78
-24
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+54
-24
RelationsTest.php
tests/RelationsTest.php
+24
-0
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
b9e1efe8
...
...
@@ -146,9 +146,10 @@ class EmbedsMany extends Relation {
}
$records
=
$this
->
getEmbeddedRecords
();
$primaryKey
=
$this
->
related
->
getKeyName
();
// Traverse all embedded records and find the first record
// that matches the given primary key.
$record
=
array_first
(
$records
,
function
(
$itemKey
,
$record
)
use
(
$primaryKey
,
$id
)
{
return
$record
[
$primaryKey
]
==
$id
;
...
...
@@ -283,6 +284,7 @@ class EmbedsMany extends Relation {
{
$ids
=
$this
->
getIdsArrayFrom
(
$ids
);
// Get all models matching the given ids.
$models
=
$this
->
get
()
->
only
(
$ids
);
// Pull the documents from the database.
...
...
@@ -302,21 +304,19 @@ class EmbedsMany extends Relation {
{
$ids
=
$this
->
getIdsArrayFrom
(
$ids
);
$records
=
$this
->
getEmbeddedRecords
();
$primaryKey
=
$this
->
related
->
getKeyName
();
// Get existing embedded documents.
$documents
=
$this
->
getEmbeddedRecords
();
// Remove the document from the parent model.
foreach
(
$
documents
as
$i
=>
$document
)
foreach
(
$
records
as
$i
=>
$record
)
{
if
(
in_array
(
$
document
[
$primaryKey
],
$ids
))
if
(
in_array
(
$
record
[
$primaryKey
],
$ids
))
{
unset
(
$
document
s
[
$i
]);
unset
(
$
record
s
[
$i
]);
}
}
$this
->
setEmbeddedRecords
(
$
document
s
);
$this
->
setEmbeddedRecords
(
$
record
s
);
// 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
...
...
@@ -325,7 +325,32 @@ class EmbedsMany extends Relation {
}
/**
* Delete alias.
* Delete all embedded models.
*
* @return int
*/
public
function
delete
()
{
// Overwrite the local key with an empty array.
$result
=
$this
->
query
->
update
(
array
(
$this
->
localKey
=>
array
()));
// If the update query was successful, we will remove the embedded records
// of the parent instance.
if
(
$result
)
{
$count
=
$this
->
count
();
$this
->
setEmbeddedRecords
(
array
());
// Return the number of deleted embedded records.
return
$count
;
}
return
$result
;
}
/**
* Destroy alias.
*
* @param mixed $ids
* @return int
...
...
@@ -356,14 +381,20 @@ class EmbedsMany extends Relation {
{
if
(
$this
->
fireModelEvent
(
$model
,
'creating'
)
===
false
)
return
false
;
// Associate the new model to the parent.
$this
->
associateNew
(
$model
);
// Create a new key if needed.
if
(
!
$model
->
getAttribute
(
'_id'
))
{
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
}
// Push the new model to the database.
$result
=
$this
->
query
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
);
if
(
$result
)
{
// Associate the new model to the parent.
$this
->
associateNew
(
$model
);
$this
->
fireModelEvent
(
$model
,
'created'
,
false
);
return
$model
;
...
...
@@ -382,9 +413,6 @@ class EmbedsMany extends Relation {
{
if
(
$this
->
fireModelEvent
(
$model
,
'updating'
)
===
false
)
return
false
;
// Update the related model in the parent instance
$this
->
associateExisting
(
$model
);
// Get the correct foreign key value.
$id
=
$this
->
getForeignKeyValue
(
$model
);
...
...
@@ -394,6 +422,9 @@ class EmbedsMany extends Relation {
if
(
$result
)
{
// Update the related model in the parent instance
$this
->
associateExisting
(
$model
);
$this
->
fireModelEvent
(
$model
,
'updated'
,
false
);
return
$model
;
...
...
@@ -438,18 +469,18 @@ class EmbedsMany extends Relation {
*/
protected
function
associateNew
(
$model
)
{
// Create a new key.
// Create a new key
if needed
.
if
(
!
$model
->
getAttribute
(
'_id'
))
{
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
}
$
document
s
=
$this
->
getEmbeddedRecords
();
$
record
s
=
$this
->
getEmbeddedRecords
();
// Add the document to the parent model.
$
document
s
[]
=
$model
->
getAttributes
();
$
record
s
[]
=
$model
->
getAttributes
();
$this
->
setEmbeddedRecords
(
$
document
s
);
$this
->
setEmbeddedRecords
(
$
record
s
);
// Mark the model as existing.
$model
->
exists
=
true
;
...
...
@@ -466,23 +497,22 @@ class EmbedsMany extends Relation {
protected
function
associateExisting
(
$model
)
{
// Get existing embedded documents.
$
document
s
=
$this
->
getEmbeddedRecords
();
$
record
s
=
$this
->
getEmbeddedRecords
();
$primaryKey
=
$this
->
related
->
getKeyName
();
$key
=
$model
->
getKey
();
// Replace the document in the parent model.
foreach
(
$
documents
as
&
$document
)
foreach
(
$
records
as
&
$record
)
{
if
(
$
document
[
$primaryKey
]
==
$key
)
if
(
$
record
[
$primaryKey
]
==
$key
)
{
$
document
=
$model
->
getAttributes
();
$
record
=
$model
->
getAttributes
();
break
;
}
}
$this
->
setEmbeddedRecords
(
$
document
s
);
$this
->
setEmbeddedRecords
(
$
record
s
);
return
$model
;
}
...
...
tests/RelationsTest.php
View file @
b9e1efe8
...
...
@@ -609,4 +609,28 @@ class RelationsTest extends TestCase {
$this
->
assertEquals
(
2
,
$relations
[
'addresses'
]
->
count
());
}
public
function
testEmbedsManyDelete
()
{
$user1
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user1
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'New York'
)));
$user1
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
)));
$user2
=
User
::
create
(
array
(
'name'
=>
'Jane Doe'
));
$user2
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Berlin'
)));
$user2
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
)));
$user1
->
addresses
()
->
delete
();
$this
->
assertEquals
(
0
,
$user1
->
addresses
()
->
count
());
$this
->
assertEquals
(
0
,
$user1
->
addresses
->
count
());
$this
->
assertEquals
(
2
,
$user2
->
addresses
()
->
count
());
$this
->
assertEquals
(
2
,
$user2
->
addresses
->
count
());
$user1
=
User
::
find
(
$user1
->
id
);
$user2
=
User
::
find
(
$user2
->
id
);
$this
->
assertEquals
(
0
,
$user1
->
addresses
()
->
count
());
$this
->
assertEquals
(
0
,
$user1
->
addresses
->
count
());
$this
->
assertEquals
(
2
,
$user2
->
addresses
()
->
count
());
$this
->
assertEquals
(
2
,
$user2
->
addresses
->
count
());
}
}
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