Skip to content

Commit 274a31d

Browse files
committed
Clone query instances for embedded relations
1 parent a0acb26 commit 274a31d

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function performDelete(Model $model)
9999
// Get the correct foreign key value.
100100
$foreignKey = $this->getForeignKeyValue($model);
101101

102-
$result = $this->query->pull($this->localKey, array($model->getKeyName() => $foreignKey));
102+
$result = $this->getBaseQuery()->pull($this->localKey, array($model->getKeyName() => $foreignKey));
103103

104104
if ($result) $this->dissociate($model);
105105

src/Jenssegers/Mongodb/Relations/EmbedsOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function performDelete(Model $model)
9090
}
9191

9292
// Overwrite the local key with an empty array.
93-
$result = $this->query->update(array($this->localKey => null));
93+
$result = $this->getBaseQuery()->update(array($this->localKey => null));
9494

9595
// Detach the model from its parent.
9696
if ($result) $this->dissociate();

src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function __construct(Builder $query, Model $parent, Model $related, $loca
5252
// If this is a nested relation, we need to get the parent query instead.
5353
if ($parentRelation = $this->getParentRelation())
5454
{
55-
//$this->query = $parentRelation->parent->newQuery();
5655
$this->query = $parentRelation->getQuery();
5756
}
5857

@@ -330,6 +329,30 @@ protected function getParentRelation()
330329
return $this->parent->getParentRelation();
331330
}
332331

332+
/**
333+
* Get the underlying query for the relation.
334+
*
335+
* @return \Illuminate\Database\Eloquent\Builder
336+
*/
337+
public function getQuery()
338+
{
339+
// Because we are sharing this relation instance to models, we need
340+
// to make sure we use separate query instances.
341+
return clone $this->query;
342+
}
343+
344+
/**
345+
* Get the base query builder driving the Eloquent builder.
346+
*
347+
* @return \Illuminate\Database\Query\Builder
348+
*/
349+
public function getBaseQuery()
350+
{
351+
// Because we are sharing this relation instance to models, we need
352+
// to make sure we use separate query instances.
353+
return clone $this->query->getQuery();
354+
}
355+
333356
/**
334357
* Check if this relation is nested in another relation.
335358
*

tests/EmbeddedRelationsTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,12 @@ public function testIncrementEmbedded()
690690
$user = User::where('name', 'John Doe')->first();
691691
$this->assertEquals(6, $user->addresses()->first()->visited);
692692

693+
$user = User::where('name', 'John Doe')->first();
694+
$address = $user->addresses()->first();
695+
693696
$address->decrement('visited');
694697
$this->assertEquals(5, $address->visited);
695-
//$this->assertEquals(5, $user->addresses()->first()->visited); TODO
698+
$this->assertEquals(5, $user->addresses()->first()->visited);
696699

697700
$user = User::where('name', 'John Doe')->first();
698701
$this->assertEquals(5, $user->addresses()->first()->visited);

0 commit comments

Comments
 (0)