Skip to content

Commit

Permalink
Using model scopes instead of checking the soft delete scope trait di…
Browse files Browse the repository at this point in the history
…rectly (#183)
  • Loading branch information
luisdalmolin authored Jun 26, 2024
1 parent 6de51d9 commit 3f57b39
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/Mixins/QueryRelationshipExistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public function getGroupBy()
};
}

public function getScopes()
{
return function () {
return $this->scopes;
};
}

public function getSelect()
{
return function () {
Expand Down
26 changes: 18 additions & 8 deletions src/Mixins/RelationshipsExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirschbaum\PowerJoins\Mixins;

use Illuminate\Database\Eloquent\Model;
use Stringable;
use Illuminate\Support\Str;
use Kirschbaum\PowerJoins\StaticCache;
Expand All @@ -16,6 +17,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\SoftDeletingScope;

/**
* @method \Illuminate\Database\Eloquent\Model getModel()
Expand Down Expand Up @@ -86,7 +88,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsTo()
"{$joinedTable}.{$this->ownerKey}"
);

if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull("{$joinedTable}.{$this->query->getModel()->getDeletedAtColumn()}");
}

Expand Down Expand Up @@ -139,7 +141,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
"{$joinedTable}.{$this->getRelatedPivotKeyName()}"
);

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

Expand Down Expand Up @@ -200,7 +202,7 @@ protected function performJoinForEloquentPowerJoinsForMorphToMany()
"{$joinedTable}.{$this->getRelatedPivotKeyName()}"
);

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

Expand All @@ -226,7 +228,7 @@ protected function performJoinForEloquentPowerJoinsForMorph()
"{$this->parent->getTable()}.{$this->localKey}"
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $this->getMorphClass());

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

Expand All @@ -249,6 +251,7 @@ protected function performJoinForEloquentPowerJoinsForMorph()
protected function performJoinForEloquentPowerJoinsForMorphTo()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, string $morphable = null) {
/** @var Model */
$modelInstance = new $morphable;

$builder->{$joinType}($modelInstance->getTable(), function ($join) use ($modelInstance, $callback, $disableExtraConditions) {
Expand All @@ -258,7 +261,7 @@ protected function performJoinForEloquentPowerJoinsForMorphTo()
"{$modelInstance->getTable()}.{$modelInstance->getKeyName()}"
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $modelInstance->getMorphClass());

if ($disableExtraConditions === false && $this->usesSoftDeletes($modelInstance)) {
if ($disableExtraConditions === false && $this->usesSoftDeletes($modelInstance->getScopes())) {
$join->whereNull($modelInstance->getQualifiedDeletedAtColumn());
}

Expand Down Expand Up @@ -304,7 +307,7 @@ protected function performJoinForEloquentPowerJoinsForHasMany()
"{$parentTable}.{$this->localKey}"
);

if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull(
"{$joinedTable}.{$this->query->getModel()->getDeletedAtColumn()}"
);
Expand Down Expand Up @@ -370,7 +373,7 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
"{$throughTable}.{$this->secondLocalKey}"
);

if ($this->usesSoftDeletes($this->getModel())) {
if ($this->usesSoftDeletes($this->getScopes())) {
$join->whereNull("{$farTable}.{$this->getModel()->getDeletedAtColumn()}");
}

Expand Down Expand Up @@ -408,8 +411,15 @@ public function performHavingForEloquentPowerJoins()
*/
public function usesSoftDeletes()
{
/**
* @param \Illuminate\Database\Eloquent\Model|array $model
*/
return function ($model) {
return in_array(SoftDeletes::class, class_uses_recursive($model));
if ($model instanceof Model) {
return in_array(SoftDeletes::class, class_uses_recursive($model));
}

return array_key_exists(SoftDeletingScope::class, $model);
};
}

Expand Down
13 changes: 11 additions & 2 deletions src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,27 @@ public function withTrashed(): self
*/
public function onlyTrashed(): self
{
if (! $this->getModel() || ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
if (! $this->getModel()
|| ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))
) {
return $this;
}

$this->wheres = array_map(function ($where) {
$hasCondition = null;

$this->wheres = array_map(function ($where) use (&$hasCondition) {
if ($where['type'] === 'Null' && Str::contains($where['column'], $this->getModel()->getDeletedAtColumn())) {
$where['type'] = 'NotNull';
$hasCondition = true;
}

return $where;
}, $this->wheres);

if (! $hasCondition) {
$this->whereNotNull($this->getModel()->getQualifiedDeletedAtColumn());
}

return $this;
}

Expand Down
5 changes: 3 additions & 2 deletions tests/JoinRelationshipExtraConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public function test_join_belongs_to_with_additional_conditions()
$query = Post::query()->joinRelationship('userWithTrashed')->toSql();
$posts = Post::query()->joinRelationship('userWithTrashed')->get();

$this->assertCount(1, $posts);

$this->assertStringContainsString(
'inner join "users" on "posts"."user_id" = "users"."id" and "users"."deleted_at" is null',
'inner join "users" on "posts"."user_id" = "users"."id"',
$query
);

$this->assertCount(2, $posts);
}

/** @test */
Expand Down
5 changes: 5 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function posts(): HasMany
return $this->hasMany(Post::class);
}

public function postsWithTrashed(): HasMany
{
return $this->hasMany(Post::class)->withTrashed();
}

public function publishedPosts(): HasMany
{
return $this->hasMany(Post::class)->where(function ($query) {
Expand Down
16 changes: 16 additions & 0 deletions tests/SoftDeletesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@ public function it_can_disable_soft_deletes_when_using_an_alias()
$query
);
}

public function test_it_respects_with_trashed()
{
User::query()->joinRelationship('postsWithTrashed')->get();
$sql = User::query()->joinRelationship('postsWithTrashed')->toSql();

$this->assertStringContainsString(
'inner join "posts" on "posts"."user_id" = "users"."id"',
$sql
);

$this->assertStringNotContainsString(
'"posts"."deleted_at" is null',
$sql
);
}
}

0 comments on commit 3f57b39

Please sign in to comment.