From ac8f69c623eb6af35021e40f88a00ecb3c0aa111 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Mon, 12 Sep 2022 09:52:06 +0200 Subject: [PATCH] Add cascadingEmptyTrash method and modify emptyTrash method to adapt to new changes, preserving backward compatibility. Add related info to readme. --- README.md | 4 +- src/Model/Behavior/TrashBehavior.php | 61 +++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 06df7d6..f484e86 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Model/Behavior/TrashBehavior.php b/src/Model/Behavior/TrashBehavior.php index 73e598e..ed37e5f 100644 --- a/src/Model/Behavior/TrashBehavior.php +++ b/src/Model/Behavior/TrashBehavior.php @@ -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). * @@ -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) + { + 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); + + /** @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) { + if ( + !$association + ->getTarget() + ->cascadingEmptyTrash($related) + ) { + $result = false; + } + } + } + } + } + + return $result; + } + /** * Returns a unary expression for bulk record manipulation. *