Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #145 ForceDelete functionality added #158

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return early instead of 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();
}
}
Loading