Skip to content

Commit 4a8012c

Browse files
marickvantuilMarick van Tuil
and
Marick van Tuil
authored
Correct 'through' relation alias, add missing alias to morph relations (#210)
Co-authored-by: Marick van Tuil <[email protected]>
1 parent d04e06b commit 4a8012c

File tree

7 files changed

+61
-5
lines changed

7 files changed

+61
-5
lines changed

src/JoinsHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ public function getAliasName(bool $useAlias, Relation $relation, string $relatio
188188
}
189189
}
190190

191-
$farParentTable = $relation->getFarParent()->getTable();
192-
if (isset($callback[$farParentTable])) {
193-
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $farParentTable);
194-
$callback[$farParentTable]($fakeJoinCallback);
191+
$relatedTable = $relation->getRelated()->getTable();
192+
if (isset($callback[$relatedTable])) {
193+
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $relatedTable);
194+
$callback[$relatedTable]($fakeJoinCallback);
195195

196196
if ($fakeJoinCallback->getAlias()) {
197197
$alias[1] = $fakeJoinCallback->getAlias();

src/Mixins/RelationshipsExtraMethods.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ protected function performJoinForEloquentPowerJoinsForMorphToMany()
223223
protected function performJoinForEloquentPowerJoinsForMorph()
224224
{
225225
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false) {
226-
$builder->{$joinType}($this->getModel()->getTable(), function ($join) use ($callback, $disableExtraConditions) {
226+
$builder->{$joinType}($this->getModel()->getTable(), function ($join) use ($callback, $disableExtraConditions, $alias) {
227+
if ($alias) {
228+
$join->as($alias);
229+
}
230+
227231
$join->on(
228232
"{$this->getModel()->getTable()}.{$this->getForeignKeyName()}",
229233
'=',

tests/JoinRelationshipUsingAliasTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ public function test_morph_join_using_alias()
267267
'inner join "images" as "foo" on "foo"."imageable_id" = "posts"."id" and "foo"."imageable_type" = ?',
268268
$query
269269
);
270+
271+
$query = Post::query()
272+
->joinRelationship('likes', fn ($join) => $join->as('foo'))
273+
->toSql();
274+
275+
$this->assertQueryContains('inner join likes as foo on foo.likeable_id = posts.id and foo.likeable_type = ? and foo.deleted_at is null', $query);
270276
}
271277

272278
/** @test */
@@ -302,6 +308,16 @@ public function test_join_through_model_with_soft_deletes_using_alias()
302308
$query
303309
);
304310

311+
// has one through - alias on related
312+
$query = User::query()->joinRelationship('postsThroughComments', [
313+
'posts' => fn ($join) => $join->as('post_alias'),
314+
])->toSql();
315+
316+
$this->assertQueryContains(
317+
$expected = 'select users.* from users inner join comments on comments.user_id = users.id inner join posts as post_alias on post_alias.comment_id = comments.id and post_alias.deleted_at is null where users.deleted_at is null',
318+
$query
319+
);
320+
305321
// has many through
306322
$query = User::query()->joinRelationship('commentsThroughPosts', [
307323
'comments' => fn ($join) => $join->as('comments_alias'),

tests/Models/Like.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Kirschbaum\PowerJoins\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\MorphTo;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
8+
9+
class Like extends Model
10+
{
11+
use SoftDeletes;
12+
/** @var string */
13+
protected $table = 'likes';
14+
15+
public function likeable(): MorphTo
16+
{
17+
return $this->morphTo();
18+
}
19+
}

tests/Models/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function images(): MorphMany
5757
return $this->morphMany(Image::class, 'imageable');
5858
}
5959

60+
public function likes(): MorphMany
61+
{
62+
return $this->morphMany(Like::class, 'likeable');
63+
}
64+
6065
public function coverImages(): MorphMany
6166
{
6267
return $this->morphMany(Image::class, 'imageable')->where('cover', true);

tests/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public function commentsThroughPosts(): HasManyThrough
6868
return $this->hasManyThrough(Comment::class, Post::class);
6969
}
7070

71+
public function postsThroughComments(): HasManyThrough
72+
{
73+
return $this->hasManyThrough(Post::class, Comment::class);
74+
}
75+
7176
public function images(): MorphMany
7277
{
7378
return $this->morphMany(Image::class, 'imageable');

tests/database/migrations/2020_03_16_000000_create_tables.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public function up()
9494
$table->softDeletes();
9595
});
9696

97+
Schema::create('likes', function (Blueprint $table) {
98+
$table->increments('id');
99+
$table->morphs('likeable');
100+
$table->timestamps();
101+
$table->softDeletes();
102+
});
103+
97104
Schema::create('tags', function (Blueprint $table) {
98105
$table->increments('id');
99106
$table->string('name');

0 commit comments

Comments
 (0)