Skip to content

Commit

Permalink
fix: allow non nested joins to specify pivot information (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolGoose authored Mar 19, 2024
1 parent 01e70ed commit 571f09c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
23 changes: 15 additions & 8 deletions src/Mixins/JoinRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ public function joinRelationship(): Closure
return $this;
}

$relationCallback = $callback;
if ($callback && is_array($callback) && isset($callback[$relationName]) && is_array($callback[$relationName])) {
$relationCallback = $callback[$relationName];
}

$relation = $this->getModel()->{$relationName}();
$relationQuery = $relation->getQuery();
$alias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relationQuery->getModel()->getTable(),
$callback
$relationCallback
);

if ($relation instanceof BelongsToMany && !is_array($alias)) {
Expand All @@ -131,12 +136,13 @@ public function joinRelationship(): Closure
$relation,
$relationName,
$relation->getTable(),
$callback
$relationCallback
);
$alias = [$extraAlias, $alias];
}

$aliasString = is_array($alias) ? implode('.', $alias) : $alias;
$useAlias = $alias ? true : $useAlias;

$relationJoinCache = $alias
? "{$aliasString}.{$relationQuery->getModel()->getTable()}.{$relationName}"
Expand All @@ -146,21 +152,23 @@ public function joinRelationship(): Closure
return $this;
}

if ($useAlias) {
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}

$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
StaticCache::clear();

$relation->performJoinForEloquentPowerJoins(
builder: $this,
joinType: $joinType,
callback: $callback,
callback: $relationCallback,
alias: $alias,
disableExtraConditions: $disableExtraConditions,
morphable: $morphable,
);

return $this;

};
}

Expand Down Expand Up @@ -353,7 +361,8 @@ public function orderByPowerJoins(): Closure
}, $this->getModel());

if ($aggregation) {
$aliasName = sprintf('%s_%s_%s',
$aliasName = sprintf(
'%s_%s_%s',
$latestRelationshipModel->getTable(),
$column,
$aggregation
Expand Down Expand Up @@ -382,7 +391,6 @@ public function orderByPowerJoins(): Closure
}
return $this;
};

}

public function orderByLeftPowerJoins(): Closure
Expand Down Expand Up @@ -517,7 +525,7 @@ public function hasNestedUsingJoins(): Closure
foreach ($relations as $index => $relation) {
$relationName = $relation;

if (! $latestRelation) {
if (!$latestRelation) {
$relation = $this->getRelationWithoutConstraintsProxy($relation);
} else {
$relation = $latestRelation->getModel()->query()->getRelationWithoutConstraintsProxy($relation);
Expand All @@ -540,7 +548,6 @@ public function powerJoinDoesntHave(): Closure
return function ($relation, $boolean = 'and', Closure $callback = null) {
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
};

}

public function powerJoinWhereHas(): Closure
Expand Down
29 changes: 25 additions & 4 deletions tests/JoinRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ public function test_join_has_many_through_relationship_with_alias()
{
$query = User::joinRelationship('commentsThroughPosts.user', [
'commentsThroughPosts' => [
'posts' => fn($join) => $join->as('posts_alias'),
'comments' => fn($join) => $join->as('comments_alias'),
'posts' => fn ($join) => $join->as('posts_alias'),
'comments' => fn ($join) => $join->as('comments_alias'),
],
])->toSql();

Expand All @@ -558,8 +558,8 @@ public function test_join_belongs_to_many_relationship_with_alias()
{
$query = Group::joinRelationship('posts.user', [
'posts' => [
'posts' => fn($join) => $join->as('posts_alias'),
'post_groups' => fn($join) => $join->as('post_groups_alias'),
'posts' => fn ($join) => $join->as('posts_alias'),
'post_groups' => fn ($join) => $join->as('post_groups_alias'),
],
])->toSql();

Expand All @@ -574,6 +574,27 @@ public function test_join_belongs_to_many_relationship_with_alias()
);
}

/** @test */
public function test_join_belongs_to_many_with_alias()
{
$query = Group::joinRelationship('posts', [
'posts' => [
'posts' => fn ($join) => $join->as('posts_alias'),
'post_groups' => fn ($join) => $join->as('post_groups_not_nested'),
],
])->toSql();

$this->assertStringContainsString(
'inner join "posts" as "posts_alias"',
$query
);

$this->assertStringContainsString(
'inner join "post_groups" as "post_groups_not_nested"',
$query
);
}

/** @test */
public function test_it_joins_different_tables_with_same_relationship_name()
{
Expand Down

0 comments on commit 571f09c

Please sign in to comment.