From 5fa4012ebe5632e51d7e7b85cd0ef3f57450b462 Mon Sep 17 00:00:00 2001 From: Dominic Lord Date: Wed, 18 Mar 2020 11:34:32 -0400 Subject: [PATCH] Tweaking actions & models to properly clean up DB after attachment deletion --- src/Charcoal/Admin/Action/RemoveJoinAction.php | 18 +++++++++++++++--- src/Charcoal/Attachment/Object/Attachment.php | 16 +++++++--------- .../Attachment/Traits/AttachmentAwareTrait.php | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/Charcoal/Admin/Action/RemoveJoinAction.php b/src/Charcoal/Admin/Action/RemoveJoinAction.php index 702f18c..9e56219 100644 --- a/src/Charcoal/Admin/Action/RemoveJoinAction.php +++ b/src/Charcoal/Admin/Action/RemoveJoinAction.php @@ -15,6 +15,7 @@ use Charcoal\Loader\CollectionLoader; // From 'charcoal-attachment' +use Charcoal\Attachment\Object\Attachment; use Charcoal\Attachment\Object\Join; /** @@ -72,11 +73,22 @@ public function run(RequestInterface $request, ResponseInterface $response) ->addFilter('attachment_id', $attachmentId) ->addFilter('group', $group); - $existing_joins = $loader->load(); + $existingJoins = $loader->load(); // Should be just one, tho. - foreach ($existing_joins as $j) { - $j->delete(); + foreach ($existingJoins as $joinModel) { + $joinModel->delete(); + } + + // Try loading the attachment + try { + $attachment = $this->modelFactory()->create(Attachment::class)->load($attachmentId); + if ($attachment['id'] !== null) { + $attachment->delete(); + } + } catch (Exception $error) { + $this->setSuccess(false); + return $response; } $this->setSuccess(true); diff --git a/src/Charcoal/Attachment/Object/Attachment.php b/src/Charcoal/Attachment/Object/Attachment.php index a4d8f5c..cc4cd9f 100644 --- a/src/Charcoal/Attachment/Object/Attachment.php +++ b/src/Charcoal/Attachment/Object/Attachment.php @@ -26,7 +26,6 @@ // From 'charcoal-attachment' use Charcoal\Attachment\Interfaces\AttachableInterface; use Charcoal\Attachment\Interfaces\AttachmentContainerInterface; - use Charcoal\Attachment\Object\File; use Charcoal\Attachment\Object\Image; use Charcoal\Attachment\Object\Text; @@ -923,15 +922,14 @@ public function setPresenter($presenter) */ public function preDelete() { - $attId = $this->id(); - $joinProto = $this->modelFactory()->get(Join::class); - $loader = $this->collectionLoader(); - $loader->setModel($joinProto); - - $collection = $loader->addFilter('attachment_id', $attId)->load(); + $joinCollection = $this->collectionLoader() + ->reset() + ->setModel(Join::class) + ->addFilter('attachment_id', $this['id']) + ->load(); - foreach ($collection as $obj) { - $obj->delete(); + foreach ($joinCollection as $joinModel) { + $joinModel->delete(); } return parent::preDelete(); diff --git a/src/Charcoal/Attachment/Traits/AttachmentAwareTrait.php b/src/Charcoal/Attachment/Traits/AttachmentAwareTrait.php index c361b3d..f15ccfc 100644 --- a/src/Charcoal/Attachment/Traits/AttachmentAwareTrait.php +++ b/src/Charcoal/Attachment/Traits/AttachmentAwareTrait.php @@ -341,6 +341,21 @@ public function removeAttachmentJoins() return true; } + /** + * Delete the objects associated to the current object. + * + * @param array $options Filter the attachments by an option list. + * @return boolean + */ + public function deleteAttachments(array $options = []) + { + foreach ($this->getAttachments($options) as $attachment) { + $attachment->delete(); + } + + return true; + } + /** * Retrieve the attachment widget. *