Skip to content

Commit 4c5c1c4

Browse files
authored
Merge pull request #57 from kirschbaum-development/issue-53
Fix #53
2 parents a2ba04a + ed938b3 commit 4c5c1c4

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ jobs:
99
strategy:
1010
fail-fast: true
1111
matrix:
12-
php: [7.3, 7.4]
13-
laravel: [6.*, 7.*, 8.*]
12+
php: [7.4]
13+
laravel: [7.*, 8.*]
1414
include:
1515
- laravel: 8.*
1616
testbench: 6.*
1717
- laravel: 7.*
1818
testbench: 5.*
19-
- laravel: 6.*
20-
testbench: 4.*
2119

2220
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
2321

src/Mixins/RelationshipsExtraMethods.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Kirschbaum\PowerJoins\PowerJoinClause;
1313
use Kirschbaum\PowerJoins\PowerJoins;
1414

15+
/**
16+
* @method getModel
17+
*/
1518
class RelationshipsExtraMethods
1619
{
1720
/**
@@ -78,7 +81,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
7881
[$alias1, $alias2] = $alias;
7982

8083
$joinedTable = $alias1 ?: $this->getTable();
81-
$parentTable = $this->getTableOrAliasForModel($this->parent)[1] ?? $this->parent->getTable();
84+
$parentTable = $this->getTableOrAliasForModel($this->parent) ?? $this->parent->getTable();
8285

8386
$builder->{$joinType}($this->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias1) {
8487
if ($alias1) {
@@ -224,6 +227,10 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
224227
if (is_array($callback) && isset($callback[$this->getThroughParent()->getTable()])) {
225228
$callback[$this->getThroughParent()->getTable()]($join);
226229
}
230+
231+
if ($callback && is_callable($callback)) {
232+
$callback($join);
233+
}
227234
}, $this->getThroughParent());
228235

229236
$builder->{$joinType}($this->getModel()->getTable(), function (PowerJoinClause $join) use ($callback, $throughTable, $farTable, $alias1, $alias2) {

src/PowerJoinClause.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function as(string $alias)
5858
$this->table = sprintf('%s as %s', $this->table, $alias);
5959
$this->useTableAliasInConditions();
6060

61+
if ($this->model) {
62+
PowerJoins::$powerJoinAliasesCache[spl_object_id($this->model)] = $alias;
63+
}
64+
6165
return $this;
6266
}
6367

tests/JoinRelationshipTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,16 @@ public function test_it_doesnt_join_the_same_relationship_twice_with_complex_nes
320320
->toSql();
321321

322322
// making sure it doesn't throw any errors
323-
User::query()->select('users.*')->joinRelationship('posts.comments')->joinRelationship('posts.images')->get();
323+
User::query()
324+
->select('users.*')
325+
->leftJoinRelationship('posts.comments')
326+
->leftJoinRelationship('posts.images')
327+
->leftJoinRelationship('posts.category')
328+
->leftJoinRelationship('posts.category.parent', [
329+
'parent' => function ($join) {
330+
$join->as('category_parent');
331+
},
332+
])->get();
324333

325334
$this->assertStringContainsString(
326335
'left join "posts" on "posts"."user_id" = "users"."id"',
@@ -555,4 +564,16 @@ public function test_passing_where_closure_inside_join_callback()
555564
$sql
556565
);
557566
}
567+
568+
/** @test */
569+
public function test_nested_join_with_aliases()
570+
{
571+
$query = Post::query()
572+
->where('posts.id', '>', 10)
573+
->joinRelationship('category.parent', [
574+
'category' => fn ($join) => $join->as('category_alias'),
575+
'parent' => fn ($join) => $join->as('parent_alias'),
576+
])
577+
->get();
578+
}
558579
}

tests/JoinRelationshipUsingAliasTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public function test_alias_for_belongs_to_many_nested()
130130
$user1->groups()->attach($group);
131131
$group->posts()->attach($post);
132132

133-
$users = User::query()->joinRelationshipUsingAlias('groups.posts')->get();
134133
$query = User::query()->joinRelationshipUsingAlias('groups.posts')->toSql();
134+
$users = User::query()->joinRelationshipUsingAlias('groups.posts')->get();
135135

136136
$this->assertCount(1, $users);
137137
$this->assertEquals($user1->id, $users->first()->id);

0 commit comments

Comments
 (0)