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
415ed78e
Commit
415ed78e
authored
Aug 09, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #244
parent
ea9a7ced
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
0 deletions
+87
-0
Builder.php
src/Jenssegers/Mongodb/Eloquent/Builder.php
+58
-0
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+8
-0
EmbedsOne.php
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
+8
-0
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+13
-0
No files found.
src/Jenssegers/Mongodb/Eloquent/Builder.php
View file @
415ed78e
...
...
@@ -96,6 +96,64 @@ class Builder extends EloquentBuilder {
return
parent
::
delete
();
}
/**
* Increment a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public
function
increment
(
$column
,
$amount
=
1
,
array
$extra
=
array
())
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
if
(
$relation
=
$this
->
model
->
getParent
())
{
$value
=
$this
->
model
->
{
$column
};
// When doing increment and decrements, Eloquent will automatically
// sync the original attributes. We need to change the attribute
// temporary in order to trigger an update query.
$this
->
model
->
{
$column
}
=
null
;
$this
->
model
->
syncOriginalAttribute
(
$column
);
$result
=
$this
->
model
->
update
(
array
(
$column
=>
$value
));
return
$result
;
}
return
parent
::
increment
(
$column
,
$amount
,
$extra
);
}
/**
* Decrement a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public
function
decrement
(
$column
,
$amount
=
1
,
array
$extra
=
array
())
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
if
(
$relation
=
$this
->
model
->
getParent
())
{
$value
=
$this
->
model
->
{
$column
};
// When doing increment and decrements, Eloquent will automatically
// sync the original attributes. We need to change the attribute
// temporary in order to trigger an update query.
$this
->
model
->
{
$column
}
=
null
;
$this
->
model
->
syncOriginalAttribute
(
$column
);
return
$this
->
model
->
update
(
array
(
$column
=>
$value
));
}
return
parent
::
decrement
(
$column
,
$amount
,
$extra
);
}
/**
* Add the "has" condition where clause to the query.
*
...
...
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
415ed78e
...
...
@@ -89,6 +89,14 @@ class EmbedsMany extends EmbedsOneOrMany {
*/
public
function
performDelete
(
Model
$model
)
{
// For deeply nested documents, let the parent handle the changes.
if
(
$this
->
isNested
())
{
$this
->
dissociate
(
$model
);
return
$this
->
parent
->
save
();
}
// Get the correct foreign key value.
$foreignKey
=
$this
->
getForeignKeyValue
(
$model
);
...
...
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
View file @
415ed78e
...
...
@@ -82,6 +82,14 @@ class EmbedsOne extends EmbedsOneOrMany {
*/
public
function
performDelete
(
Model
$model
)
{
// For deeply nested documents, let the parent handle the changes.
if
(
$this
->
isNested
())
{
$this
->
dissociate
(
$model
);
return
$this
->
parent
->
save
();
}
// Overwrite the local key with an empty array.
$result
=
$this
->
query
->
update
(
array
(
$this
->
localKey
=>
null
));
...
...
tests/EmbeddedRelationsTest.php
View file @
415ed78e
...
...
@@ -651,4 +651,17 @@ class EmbeddedRelationsTest extends TestCase {
$this
->
assertEquals
(
1
,
$user
->
addresses
()
->
count
());
}
public
function
testIncrementEmbedded
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$address
=
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'New York'
,
'visited'
=>
5
));
$address
->
increment
(
'visited'
);
$this
->
assertEquals
(
6
,
$address
->
visited
);
$this
->
assertEquals
(
6
,
$user
->
addresses
()
->
first
()
->
visited
);
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
6
,
$user
->
addresses
()
->
first
()
->
visited
);
}
}
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