Skip to content

Commit

Permalink
feat: #145 ForceDelete functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogarine committed Oct 28, 2024
1 parent 56d16b1 commit 0707dcf
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/SoftCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected function getMorphManyData($relation, $foreignKeyIds)
}

/**
* Execute delete, or restore.
* Execute delete or restore or forcedelete.
*
* @param Illuminate\Database\Eloquent\Relations\Relation $relation
* @param string $foreignKey
Expand All @@ -211,7 +211,13 @@ protected function execute($relation, $foreignKey, $foreignKeyIds, $affectedRows
$relationModel = $relationModel->whereIn($foreignKey, $foreignKeyIds)->limit($affectedRows);

$this->run($relationModel->get([$relationModel->getModel()->getKeyName()]));
$relationModel->{$this->direction}($this->directionData);

// COMMIT : force delete when parent model "isForceDeleting = true"
if ($this->isForceDeleting($relation)) {
$relationModel->forceDelete();
} else {
$relationModel->{$this->direction}($this->directionData);
}
}
}

Expand Down Expand Up @@ -251,7 +257,12 @@ protected function validateRelation($model, $relation)
protected function affectedRows($relation, $foreignKey, $foreignKeyIds)
{
$relationModel = $relation->getQuery()->getModel();
$relationModel = $this->withTrashed($relationModel::query());
$relationModel = new $relationModel();

// COMMIT : retreive relation trashed items when parent model "isForceDeleting = true"
if ($this->direction !== 'delete' || $this->isForceDeleting($relation)) {
$relationModel = $relationModel->withTrashed();
}

return $relationModel->whereIn($foreignKey, $foreignKeyIds)->count();
}
Expand Down Expand Up @@ -291,4 +302,15 @@ protected function withTrashed(Builder $builder): Builder

return $builder;
}

/**
* COMMIT
* Check if parent has a force delete enabled
* @return boolean
*/
protected function isForceDeleting($relation)
{
$parent = $relation->getParent();
return property_exists($parent, 'forceDeleting') && $parent->isForceDeleting();
}
}

0 comments on commit 0707dcf

Please sign in to comment.