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

Add cascadingEmptyTrash method… #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ as part of certain calls.

### Extras

- **emptyTrash()** - permanently deletes all trashed records.
- **emptyTrash($entity = null)** - permanently deletes one (or all) trashed records.
- **cascadingEmptyTrash($entity = null)** - permanently deletes one (or all) trashed records including
those of dependent associations.
- **restoreTrash($entity = null, array $options = [])** - restores one (or all) trashed records.
- **cascadingRestoreTrash($entity = null, array $options = [])** - restores one (or all) trashed records including
those of dependent associations.
Expand Down
61 changes: 51 additions & 10 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,6 @@ public function trashAll($conditions): int
);
}

/**
* Deletes all rows marked as `trashed`.
*
* @return int
*/
public function emptyTrash(): int
{
return $this->_table->deleteAll($this->_getUnaryExpression());
}

/**
* Restores all (or given) trashed row(s).
*
Expand Down Expand Up @@ -306,6 +296,57 @@ public function cascadingRestoreTrash(?EntityInterface $entity = null, array $op
return $result;
}

/**
* Deletes all (or given) rows marked as `trashed`.
*
* @param \Cake\Datasource\EntityInterface|null $entity to delete.
* @return bool|\Cake\Datasource\EntityInterface|int
*/
public function emptyTrash(?EntityInterface $entity = null)
Copy link
Member

Choose a reason for hiding this comment

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

Don't mix deletion of single and multiple records, it makes the API ugly. Static analyzers won't be able to detect the proper return type.

{
if ($entity instanceof EntityInterface) {
return $this->_table->deleteAll(['id' => $entity->id]) ? $entity : false;
}

return $this->_table->deleteAll($this->_getUnaryExpression());
}

/**
* Delete an item from trashed status and all its related data
*
* @param \Cake\Datasource\EntityInterface $entity Entity instance
* @return bool|\Cake\Datasource\EntityInterface|int
*/
public function cascadingEmptyTrash(?EntityInterface $entity = null)
{
$result = $this->emptyTrash($entity);
Copy link
Member

Choose a reason for hiding this comment

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

The dependent records should be deleted before parents, otherwise you will get delete failures if foreign key constraint is set on the dependent table.


/** @var \Cake\ORM\Association $association */
foreach ($this->_table->associations() as $association) {
if ($this->_isRecursable($association, $this->_table)) {
if ($entity === null) {
$result += $association->getTarget()->cascadingEmptyTrash(null);
} else {
$foreignKey = (array)$association->getForeignKey();
$bindingKey = (array)$association->getBindingKey();
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));

foreach ($association->find('onlyTrashed')->where($conditions) as $related) {
Copy link
Member

Choose a reason for hiding this comment

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

If a record is being hard deleted then all it's dependent records should be hard deleted, not just the ones which are trashed, otherwise you could end up with orphaned records. So the withTrashed finder should be used here not onlyTrashed.

if (
!$association
->getTarget()
->cascadingEmptyTrash($related)
) {
$result = false;
}
}
}
}
}

return $result;
}

/**
* Returns a unary expression for bulk record manipulation.
*
Expand Down