Commit 274a31da authored by Jens Segers's avatar Jens Segers

Clone query instances for embedded relations

parent a0acb263
......@@ -99,7 +99,7 @@ class EmbedsMany extends EmbedsOneOrMany {
// Get the correct foreign key value.
$foreignKey = $this->getForeignKeyValue($model);
$result = $this->query->pull($this->localKey, array($model->getKeyName() => $foreignKey));
$result = $this->getBaseQuery()->pull($this->localKey, array($model->getKeyName() => $foreignKey));
if ($result) $this->dissociate($model);
......
......@@ -90,7 +90,7 @@ class EmbedsOne extends EmbedsOneOrMany {
}
// Overwrite the local key with an empty array.
$result = $this->query->update(array($this->localKey => null));
$result = $this->getBaseQuery()->update(array($this->localKey => null));
// Detach the model from its parent.
if ($result) $this->dissociate();
......
......@@ -52,7 +52,6 @@ abstract class EmbedsOneOrMany extends Relation {
// If this is a nested relation, we need to get the parent query instead.
if ($parentRelation = $this->getParentRelation())
{
//$this->query = $parentRelation->parent->newQuery();
$this->query = $parentRelation->getQuery();
}
......@@ -330,6 +329,30 @@ abstract class EmbedsOneOrMany extends Relation {
return $this->parent->getParentRelation();
}
/**
* Get the underlying query for the relation.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getQuery()
{
// Because we are sharing this relation instance to models, we need
// to make sure we use separate query instances.
return clone $this->query;
}
/**
* Get the base query builder driving the Eloquent builder.
*
* @return \Illuminate\Database\Query\Builder
*/
public function getBaseQuery()
{
// Because we are sharing this relation instance to models, we need
// to make sure we use separate query instances.
return clone $this->query->getQuery();
}
/**
* Check if this relation is nested in another relation.
*
......
......@@ -690,9 +690,12 @@ class EmbeddedRelationsTest extends TestCase {
$user = User::where('name', 'John Doe')->first();
$this->assertEquals(6, $user->addresses()->first()->visited);
$user = User::where('name', 'John Doe')->first();
$address = $user->addresses()->first();
$address->decrement('visited');
$this->assertEquals(5, $address->visited);
//$this->assertEquals(5, $user->addresses()->first()->visited); TODO
$this->assertEquals(5, $user->addresses()->first()->visited);
$user = User::where('name', 'John Doe')->first();
$this->assertEquals(5, $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