Commit 415ed78e authored by Jens Segers's avatar Jens Segers

Fixes #244

parent ea9a7ced
......@@ -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.
*
......
......@@ -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);
......
......@@ -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));
......
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment