Skip to content

Commit 85ff768

Browse files
committed
Fixed morph relationships using alias
1 parent b9aa4c2 commit 85ff768

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

src/Mixins/RelationshipsExtraMethods.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected function performJoinForEloquentPowerJoinsForMorph()
141141
"{$this->getModel()->getTable()}.{$this->getForeignKeyName()}",
142142
'=',
143143
"{$this->parent->getTable()}.{$this->localKey}"
144-
)->where($this->getMorphType(), '=', $this->getMorphClass());
144+
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $this->getMorphClass());
145145

146146
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
147147
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());

src/PowerJoinClause.php

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,56 @@ protected function useTableAliasInConditions(): self
107107
}
108108

109109
$this->wheres = collect($this->wheres)->filter(function ($where) {
110-
return in_array($where['type'] ?? '', ['Column']);
110+
return in_array($where['type'] ?? '', ['Column', 'Basic']);
111111
})->map(function ($where) {
112-
// dd($where);
113112
$key = $this->model->getKeyName();
114113
$table = $this->tableName;
114+
$replaceMethod = sprintf('useAliasInWhere%sType', ucfirst($where['type']));
115115

116-
// if it was already replaced, skip
117-
if (Str::startsWith($where['first'] . '.', $this->alias . '.') || Str::startsWith($where['second'] . '.', $this->alias . '.')) {
118-
return $where;
119-
}
116+
return $this->{$replaceMethod}($where);
117+
})->toArray();
120118

121-
if (Str::contains($where['first'], $table) && Str::contains($where['second'], $table)) {
122-
// if joining the same table, only replace the correct table.key pair
123-
$where['first'] = str_replace($table . '.' . $key, $this->alias . '.' . $key, $where['first']);
124-
$where['second'] = str_replace($table . '.' . $key, $this->alias . '.' . $key, $where['second']);
125-
} else {
126-
$where['first'] = str_replace($table . '.', $this->alias . '.', $where['first']);
127-
$where['second'] = str_replace($table . '.', $this->alias . '.', $where['second']);
128-
}
119+
return $this;
120+
}
129121

122+
protected function useAliasInWhereColumnType(array $where): array
123+
{
124+
$key = $this->model->getKeyName();
125+
$table = $this->tableName;
126+
127+
// if it was already replaced, skip
128+
if (Str::startsWith($where['first'] . '.', $this->alias . '.') || Str::startsWith($where['second'] . '.', $this->alias . '.')) {
130129
return $where;
131-
})->toArray();
130+
}
132131

133-
return $this;
132+
if (Str::contains($where['first'], $table) && Str::contains($where['second'], $table)) {
133+
// if joining the same table, only replace the correct table.key pair
134+
$where['first'] = str_replace($table . '.' . $key, $this->alias . '.' . $key, $where['first']);
135+
$where['second'] = str_replace($table . '.' . $key, $this->alias . '.' . $key, $where['second']);
136+
} else {
137+
$where['first'] = str_replace($table . '.', $this->alias . '.', $where['first']);
138+
$where['second'] = str_replace($table . '.', $this->alias . '.', $where['second']);
139+
}
140+
141+
return $where;
142+
}
143+
144+
protected function useAliasInWhereBasicType(array $where): array
145+
{
146+
$table = $this->tableName;
147+
148+
if (Str::startsWith($where['column'] . '.', $this->alias . '.')) {
149+
return $where;
150+
}
151+
152+
if (Str::contains($where['column'], $table)) {
153+
// if joining the same table, only replace the correct table.key pair
154+
$where['column'] = str_replace($table . '.', $this->alias . '.', $where['column']);
155+
} else {
156+
$where['column'] = str_replace($table . '.', $this->alias . '.', $where['column']);
157+
}
158+
159+
return $where;
134160
}
135161

136162
public function whereNull($columns, $boolean = 'and', $not = false)

tests/JoinRelationshipExtraConditionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function test_extra_conditions_in_morph_many()
179179
$this->assertCount(1, $posts);
180180

181181
$this->assertStringContainsString(
182-
'inner join "images" on "images"."imageable_id" = "posts"."id" and "imageable_type" = ? and "cover" = ?',
182+
'inner join "images" on "images"."imageable_id" = "posts"."id" and "images"."imageable_type" = ? and "cover" = ?',
183183
$query
184184
);
185185
}

tests/JoinRelationshipTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function test_join_morph_relationship()
103103

104104
$this->assertCount(5, $posts);
105105
$this->assertStringContainsString(
106-
'inner join "images" on "images"."imageable_id" = "posts"."id" and "imageable_type" = ?',
106+
'inner join "images" on "images"."imageable_id" = "posts"."id" and "images"."imageable_type" = ?',
107107
$query
108108
);
109109
}
@@ -119,7 +119,7 @@ public function test_join_morph_nested_relationship()
119119
);
120120

121121
$this->assertStringContainsString(
122-
'inner join "images" on "images"."imageable_id" = "posts"."id" and "imageable_type" = ?',
122+
'inner join "images" on "images"."imageable_id" = "posts"."id" and "images"."imageable_type" = ?',
123123
$query
124124
);
125125
}
@@ -303,7 +303,7 @@ public function test_it_doesnt_join_the_same_relationship_twice_with_nested()
303303
);
304304

305305
$this->assertStringContainsString(
306-
'inner join "images" on "images"."imageable_id" = "posts"."id" and "imageable_type" = ?',
306+
'inner join "images" on "images"."imageable_id" = "posts"."id" and "images"."imageable_type" = ?',
307307
$query
308308
);
309309
}
@@ -353,7 +353,7 @@ public function test_it_doesnt_join_the_same_relationship_twice_with_complex_nes
353353
);
354354

355355
$this->assertStringContainsString(
356-
'left join "images" on "images"."imageable_id" = "posts"."id" and "imageable_type" = ?',
356+
'left join "images" on "images"."imageable_id" = "posts"."id" and "images"."imageable_type" = ?',
357357
$query
358358
);
359359

tests/JoinRelationshipUsingAliasTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,23 @@ public function test_joining_deep_many_to_many_relation_using_same_base_table()
248248
$sql = $query->toSql();
249249
$this->assertStringContainsString('inner join "posts" as "post_alias" on "post_alias"."id" = "post_groups_alias"."post_id"', $sql);
250250
}
251+
252+
/** @test */
253+
public function test_morph_join_using_alias()
254+
{
255+
$query = Post::query()
256+
->with(['images'])
257+
->joinRelationshipUsingAlias('images', 'foo')
258+
->toSql();
259+
260+
Post::query()
261+
->with(['images'])
262+
->joinRelationshipUsingAlias('images', 'foo')
263+
->get();
264+
265+
$this->assertStringContainsString(
266+
'inner join "images" as "foo" on "foo"."imageable_id" = "posts"."id" and "foo"."imageable_type" = ?',
267+
$query
268+
);
269+
}
251270
}

0 commit comments

Comments
 (0)