Skip to content

Commit 415ed78

Browse files
committed
Fixes #244
1 parent ea9a7ce commit 415ed78

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,64 @@ public function delete()
9696
return parent::delete();
9797
}
9898

99+
/**
100+
* Increment a column's value by a given amount.
101+
*
102+
* @param string $column
103+
* @param int $amount
104+
* @param array $extra
105+
* @return int
106+
*/
107+
public function increment($column, $amount = 1, array $extra = array())
108+
{
109+
// Intercept operations on embedded models and delegate logic
110+
// to the parent relation instance.
111+
if ($relation = $this->model->getParent())
112+
{
113+
$value = $this->model->{$column};
114+
115+
// When doing increment and decrements, Eloquent will automatically
116+
// sync the original attributes. We need to change the attribute
117+
// temporary in order to trigger an update query.
118+
$this->model->{$column} = null;
119+
$this->model->syncOriginalAttribute($column);
120+
121+
$result = $this->model->update(array($column => $value));
122+
123+
return $result;
124+
}
125+
126+
return parent::increment($column, $amount, $extra);
127+
}
128+
129+
/**
130+
* Decrement a column's value by a given amount.
131+
*
132+
* @param string $column
133+
* @param int $amount
134+
* @param array $extra
135+
* @return int
136+
*/
137+
public function decrement($column, $amount = 1, array $extra = array())
138+
{
139+
// Intercept operations on embedded models and delegate logic
140+
// to the parent relation instance.
141+
if ($relation = $this->model->getParent())
142+
{
143+
$value = $this->model->{$column};
144+
145+
// When doing increment and decrements, Eloquent will automatically
146+
// sync the original attributes. We need to change the attribute
147+
// temporary in order to trigger an update query.
148+
$this->model->{$column} = null;
149+
$this->model->syncOriginalAttribute($column);
150+
151+
return $this->model->update(array($column => $value));
152+
}
153+
154+
return parent::decrement($column, $amount, $extra);
155+
}
156+
99157
/**
100158
* Add the "has" condition where clause to the query.
101159
*

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ public function performUpdate(Model $model, array $values)
8989
*/
9090
public function performDelete(Model $model)
9191
{
92+
// For deeply nested documents, let the parent handle the changes.
93+
if ($this->isNested())
94+
{
95+
$this->dissociate($model);
96+
97+
return $this->parent->save();
98+
}
99+
92100
// Get the correct foreign key value.
93101
$foreignKey = $this->getForeignKeyValue($model);
94102

src/Jenssegers/Mongodb/Relations/EmbedsOne.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public function performUpdate(Model $model, array $values)
8282
*/
8383
public function performDelete(Model $model)
8484
{
85+
// For deeply nested documents, let the parent handle the changes.
86+
if ($this->isNested())
87+
{
88+
$this->dissociate($model);
89+
90+
return $this->parent->save();
91+
}
92+
8593
// Overwrite the local key with an empty array.
8694
$result = $this->query->update(array($this->localKey => null));
8795

tests/EmbeddedRelationsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,17 @@ public function testSaveEmptyModel()
651651
$this->assertEquals(1, $user->addresses()->count());
652652
}
653653

654+
public function testIncrementEmbedded()
655+
{
656+
$user = User::create(array('name' => 'John Doe'));
657+
$address = $user->addresses()->create(array('city' => 'New York', 'visited' => 5));
658+
659+
$address->increment('visited');
660+
$this->assertEquals(6, $address->visited);
661+
$this->assertEquals(6, $user->addresses()->first()->visited);
662+
663+
$user = User::where('name', 'John Doe')->first();
664+
$this->assertEquals(6, $user->addresses()->first()->visited);
665+
}
666+
654667
}

0 commit comments

Comments
 (0)