Skip to content

Commit 84a2478

Browse files
authored
fix: include soft delete conditions when using named aliases (#204)
* add failing test case * do not skip soft delete wheres when using alias * fix tests * linter
1 parent b092366 commit 84a2478

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

src/PowerJoinClause.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,22 @@ protected function useTableAliasInConditions(): self
111111
}
112112

113113
$this->wheres = collect($this->wheres)->filter(function ($where) {
114-
return in_array($where['type'] ?? '', ['Column', 'Basic'], true);
114+
$whereType = $where['type'] ?? '';
115+
116+
if (in_array($whereType, ['Column', 'Basic'], true)) {
117+
return true;
118+
}
119+
120+
if ($whereType === 'Null' && Str::contains($where['column'], $this->getModel()->getDeletedAtColumn())) {
121+
return true;
122+
}
123+
124+
return false;
115125
})->map(function ($where) {
126+
if ($where['type'] === 'Null') {
127+
return $where;
128+
}
129+
116130
$key = $this->model->getKeyName();
117131
$table = $this->tableName;
118132
$replaceMethod = sprintf('useAliasInWhere%sType', ucfirst($where['type']));

tests/JoinRelationshipTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ public function test_it_can_alias_belongs_to_many()
793793
);
794794

795795
$this->assertQueryContains(
796-
'inner join "posts" as "posts_1" on "posts_1"."id" = "pivot_posts_1"."post_id" and "posts_1"."id" = ?',
796+
'inner join "posts" as "posts_1" on "posts_1"."id" = "pivot_posts_1"."post_id" and "posts_1"."deleted_at" is null and "posts_1"."id" = ?',
797797
$sql
798798
);
799799
}

tests/JoinRelationshipUsingAliasTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kirschbaum\PowerJoins\Tests;
44

5+
use Kirschbaum\PowerJoins\PowerJoinClause;
56
use Kirschbaum\PowerJoins\Tests\Models\Category;
67
use Kirschbaum\PowerJoins\Tests\Models\Comment;
78
use Kirschbaum\PowerJoins\Tests\Models\Group;
@@ -267,4 +268,22 @@ public function test_morph_join_using_alias()
267268
$query
268269
);
269270
}
271+
272+
/** @test */
273+
public function test_join_model_with_soft_deletes_using_alias()
274+
{
275+
$queryA = UserProfile::query()->joinRelationshipUsingAlias('user', 'user_alias')->toSql();
276+
$queryB = UserProfile::query()->joinRelationship('user', 'user_alias')->toSql();
277+
$queryC = UserProfile::query()->joinRelationship(
278+
'user',
279+
fn (PowerJoinClause $join) => $join->as('user_alias')
280+
)->toSql();
281+
282+
$this->assertQueryContains(
283+
$expected = 'inner join "users" as "user_alias" on "user_profiles"."user_id" = "user_alias"."id" and "user_alias"."deleted_at" is null',
284+
$queryA
285+
);
286+
$this->assertQueryContains($expected, $queryB);
287+
$this->assertQueryContains($expected, $queryC);
288+
}
270289
}

tests/OrderByTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function test_order_by_relationship_with_relationship_alias()
209209
]);
210210

211211
$this->assertQueryContains(
212-
'select "users".* from "users" inner join "posts" as "posts_alias" on "posts_alias"."user_id" = "users"."id" inner join "categories" as "category_alias" on "posts_alias"."category_id" = "category_alias"."id" where "users"."deleted_at" is null order by "category_alias"."title" desc',
212+
'select "users".* from "users" inner join "posts" as "posts_alias" on "posts_alias"."user_id" = "users"."id" and "posts_alias"."deleted_at" is null inner join "categories" as "category_alias" on "posts_alias"."category_id" = "category_alias"."id" where "users"."deleted_at" is null order by "category_alias"."title" desc',
213213
$query->toSql()
214214
);
215215

0 commit comments

Comments
 (0)