Skip to content

Commit f3d4da2

Browse files
committed
Making joinRelationshipCache a private non-static property
1 parent 528fb3c commit f3d4da2

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/PowerJoins.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait PowerJoins
1515
*
1616
* @var array
1717
*/
18-
public static $joinRelationshipCache = [];
18+
private $joinRelationshipCache = [];
1919

2020
/**
2121
* Cache to not join the same relationship twice.
@@ -328,15 +328,15 @@ public function scopePowerJoinWhereHas(Builder $query, $relation, Closure $callb
328328
*/
329329
public function relationshipAlreadyJoined($relation)
330330
{
331-
return isset(PowerJoins::$joinRelationshipCache[spl_object_id($this)][$relation]);
331+
return isset($this->joinRelationshipCache[spl_object_id($this)][$relation]);
332332
}
333333

334334
/**
335335
* Marks the relationship as already joined.
336336
*/
337337
public function markRelationshipAsAlreadyJoined($relation)
338338
{
339-
PowerJoins::$joinRelationshipCache[spl_object_id($this)][$relation] = true;
339+
$this->joinRelationshipCache[spl_object_id($this)][$relation] = true;
340340
}
341341

342342
public function generateAliasForRelationship($relation, $relationName)
@@ -368,9 +368,4 @@ public function clearPowerJoinCaches()
368368

369369
return $this;
370370
}
371-
372-
public function __destruct()
373-
{
374-
PowerJoins::$joinRelationshipCache = [];
375-
}
376371
}

tests/JoinRelationshipTest.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,30 @@ public function test_join_belongs_to_many_with_callback()
247247

248248
/** @test */
249249
public function test_it_doesnt_join_the_same_relationship_twice()
250+
{
251+
$query = User::query()
252+
->select('users.*')
253+
->joinRelationship('posts')
254+
->joinRelationship('posts')
255+
->toSql();
256+
257+
// making sure it doesn't throw any errors
258+
User::query()->select('users.*')->joinRelationship('posts')->joinRelationship('posts')->get();
259+
260+
$this->assertStringContainsString(
261+
'inner join "posts" on "posts"."user_id" = "users"."id"',
262+
$query
263+
);
264+
265+
$this->assertEquals(
266+
1,
267+
substr_count($query, 'inner join "posts" on "posts"."user_id" = "users"."id"'),
268+
'It should only make 1 join with the posts table'
269+
);
270+
}
271+
272+
/** @test */
273+
public function test_it_doesnt_join_the_same_relationship_twice_with_nested()
250274
{
251275
$query = User::query()
252276
->select('users.*')
@@ -421,14 +445,4 @@ public function test_passing_where_closure_inside_join_callback()
421445
$sql
422446
);
423447
}
424-
425-
/** @test */
426-
public function test_join_cache_is_cleared_between_queries()
427-
{
428-
$query = User::query()->joinRelationship('posts')->toSql();
429-
$this->assertEmpty(PowerJoins::$joinRelationshipCache, 'Join Relationship Cache not cleared after query');
430-
431-
$query = User::query()->joinRelationship('posts')->toSql();
432-
$this->assertEmpty(PowerJoins::$joinRelationshipCache, 'Join Relationship Cache not cleared after query');
433-
}
434448
}

0 commit comments

Comments
 (0)