Skip to content

Commit e92329b

Browse files
committed
Fixed clone of queries that were duplicating joins
1 parent ef388b6 commit e92329b

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/JoinsHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ protected function __construct()
1515

1616
}
1717

18-
public static function make($builder): static
18+
public static function make(): static
1919
{
20-
return static::$instances[spl_object_id($builder)] ??= new self();
20+
$objects = array_map(fn ($object) => spl_object_id($object), func_get_args());
21+
22+
return static::$instances[implode('-', $objects)] ??= new self();
2123
}
2224

2325
/**

src/Mixins/JoinRelationship.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
/**
1818
* @mixin Builder
19+
* @method \Illuminate\Database\Eloquent\Model getModel()
20+
* @property \Illuminate\Database\Eloquent\Builder $query
1921
*/
2022
class JoinRelationship
2123
{
22-
23-
2424
/**
2525
* New clause for making joins, where we pass the model to the joiner class.
2626
*/
@@ -97,7 +97,7 @@ public function joinRelationship(): Closure
9797
) {
9898
$joinType = JoinsHelper::$joinMethodsMap[$joinType] ?? $joinType;
9999
$useAlias = is_string($callback) ? false : $useAlias;
100-
$joinHelper = JoinsHelper::make($this);
100+
$joinHelper = JoinsHelper::make($this->getModel());
101101
$callback = $joinHelper->formatJoinCallback($callback);
102102

103103
if (is_null($this->getSelect())) {
@@ -233,7 +233,7 @@ public function joinNestedRelationship(): Closure
233233
bool $disableExtraConditions = false
234234
) {
235235
$relations = explode('.', $relationships);
236-
$joinHelper = JoinsHelper::make($this);
236+
$joinHelper = JoinsHelper::make($this->getModel());
237237
/** @var Relation */
238238
$latestRelation = null;
239239

@@ -515,5 +515,4 @@ public function powerJoinWhereHas(): Closure
515515
return $this->powerJoinHas($relation, $operator, $count, 'and', $callback);
516516
};
517517
}
518-
519518
}

tests/JoinRelationshipTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,11 @@ public function test_union_query()
691691

692692
public function test_scope_inside_nested_where()
693693
{
694-
Comment::joinRelationship('post', function ($join) {
694+
Comment::query()->joinRelationship('post', function ($join) {
695695
$join->where(fn ($query) => $query->published());
696696
})->get();
697697

698-
$sql = Comment::joinRelationship('post', function ($join) {
698+
$sql = Comment::query()->joinRelationship('post', function ($join) {
699699
$join->where(fn ($query) => $query->published());
700700
})->toSql();
701701

@@ -768,4 +768,15 @@ public function test_has_one_of_many()
768768

769769
$this->assertEquals($lastComment->body, $lastCommentPost->body);
770770
}
771+
772+
public function test_join_with_clone_does_not_duplicate()
773+
{
774+
$query = Post::query();
775+
776+
$query->leftJoinRelationship('user');
777+
$clonedSql = $query->clone()->leftJoinRelationship('user')->toSql();
778+
$sql = $query->toSql();
779+
780+
$this->assertEquals($clonedSql, $sql);
781+
}
771782
}

0 commit comments

Comments
 (0)