Skip to content

Commit

Permalink
Merge pull request #57 from kirschbaum-development/issue-53
Browse files Browse the repository at this point in the history
Fix #53
  • Loading branch information
luisdalmolin committed Aug 30, 2021
2 parents a2ba04a + ed938b3 commit 4c5c1c4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4]
laravel: [6.*, 7.*, 8.*]
php: [7.4]
laravel: [7.*, 8.*]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*

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

Expand Down
9 changes: 8 additions & 1 deletion src/Mixins/RelationshipsExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\PowerJoins;

/**
* @method getModel
*/
class RelationshipsExtraMethods
{
/**
Expand Down Expand Up @@ -78,7 +81,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
[$alias1, $alias2] = $alias;

$joinedTable = $alias1 ?: $this->getTable();
$parentTable = $this->getTableOrAliasForModel($this->parent)[1] ?? $this->parent->getTable();
$parentTable = $this->getTableOrAliasForModel($this->parent) ?? $this->parent->getTable();

$builder->{$joinType}($this->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias1) {
if ($alias1) {
Expand Down Expand Up @@ -224,6 +227,10 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
if (is_array($callback) && isset($callback[$this->getThroughParent()->getTable()])) {
$callback[$this->getThroughParent()->getTable()]($join);
}

if ($callback && is_callable($callback)) {
$callback($join);
}
}, $this->getThroughParent());

$builder->{$joinType}($this->getModel()->getTable(), function (PowerJoinClause $join) use ($callback, $throughTable, $farTable, $alias1, $alias2) {
Expand Down
4 changes: 4 additions & 0 deletions src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function as(string $alias)
$this->table = sprintf('%s as %s', $this->table, $alias);
$this->useTableAliasInConditions();

if ($this->model) {
PowerJoins::$powerJoinAliasesCache[spl_object_id($this->model)] = $alias;
}

return $this;
}

Expand Down
23 changes: 22 additions & 1 deletion tests/JoinRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,16 @@ public function test_it_doesnt_join_the_same_relationship_twice_with_complex_nes
->toSql();

// making sure it doesn't throw any errors
User::query()->select('users.*')->joinRelationship('posts.comments')->joinRelationship('posts.images')->get();
User::query()
->select('users.*')
->leftJoinRelationship('posts.comments')
->leftJoinRelationship('posts.images')
->leftJoinRelationship('posts.category')
->leftJoinRelationship('posts.category.parent', [
'parent' => function ($join) {
$join->as('category_parent');
},
])->get();

$this->assertStringContainsString(
'left join "posts" on "posts"."user_id" = "users"."id"',
Expand Down Expand Up @@ -555,4 +564,16 @@ public function test_passing_where_closure_inside_join_callback()
$sql
);
}

/** @test */
public function test_nested_join_with_aliases()
{
$query = Post::query()
->where('posts.id', '>', 10)
->joinRelationship('category.parent', [
'category' => fn ($join) => $join->as('category_alias'),
'parent' => fn ($join) => $join->as('parent_alias'),
])
->get();
}
}
2 changes: 1 addition & 1 deletion tests/JoinRelationshipUsingAliasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public function test_alias_for_belongs_to_many_nested()
$user1->groups()->attach($group);
$group->posts()->attach($post);

$users = User::query()->joinRelationshipUsingAlias('groups.posts')->get();
$query = User::query()->joinRelationshipUsingAlias('groups.posts')->toSql();
$users = User::query()->joinRelationshipUsingAlias('groups.posts')->get();

$this->assertCount(1, $users);
$this->assertEquals($user1->id, $users->first()->id);
Expand Down

0 comments on commit 4c5c1c4

Please sign in to comment.