Skip to content

Commit fadc20d

Browse files
committed
Clear the joins cache before starting a join
1 parent e9d936e commit fadc20d

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/JoinsHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public static function make(): static
2929
*/
3030
private array $joinRelationshipCache = [];
3131

32-
3332
/**
3433
* Join method map.
3534
*/
@@ -117,4 +116,9 @@ public function markRelationshipAsAlreadyJoined($model, string $relation): void
117116
{
118117
$this->joinRelationshipCache[spl_object_id($model)][$relation] = true;
119118
}
119+
120+
public function clear(): void
121+
{
122+
$this->joinRelationshipCache = [];
123+
}
120124
}

src/Mixins/JoinRelationship.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public function joinRelationship(): Closure
100100
$joinHelper = JoinsHelper::make($this->getModel());
101101
$callback = $joinHelper->formatJoinCallback($callback);
102102

103+
$this->getQuery()->beforeQuery(function () use ($joinHelper) {
104+
$joinHelper->clear();
105+
});
106+
103107
if (is_null($this->getSelect())) {
104108
$this->select(sprintf('%s.*', $this->getModel()->getTable()));
105109
}

tests/JoinRelationshipTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,20 @@ public function test_join_with_clone_does_not_duplicate()
779779

780780
$this->assertEquals($clonedSql, $sql);
781781
}
782+
783+
public function test_issue_148()
784+
{
785+
for ($i = 0; $i < 12; $i++) {
786+
try {
787+
(new Post)->query()
788+
->selectRaw('users.id as user_id')
789+
->joinRelationship('user')
790+
->get();
791+
792+
$this->assertTrue(true);
793+
} catch (\Exception $e) {
794+
$this->assertTrue(false, 'If it throws an exceptions, means the already joined checks are failing');
795+
}
796+
}
797+
}
782798
}

0 commit comments

Comments
 (0)