Ensure that attachments are not used in preview when deleting, so we do not encounter integrity violations.

This fixes issue #50.
This commit is contained in:
Jan Böhmer 2020-05-13 20:46:13 +02:00
parent 7b108f8d4b
commit c0b4172f22
2 changed files with 25 additions and 0 deletions

View file

@ -111,6 +111,11 @@ abstract class AttachmentContainingDBElement extends AbstractNamedDBElement impl
{ {
$this->attachments->removeElement($attachment); $this->attachments->removeElement($attachment);
//Check if this is the master attachment -> remove it from master attachment too, or it can not be deleted from DB...
if ($attachment === $this->getMasterPictureAttachment()) {
$this->setMasterPictureAttachment(null);
}
return $this; return $this;
} }
} }

View file

@ -48,6 +48,7 @@ use App\Services\Attachments\AttachmentPathResolver;
use App\Services\Attachments\AttachmentReverseSearch; use App\Services\Attachments\AttachmentReverseSearch;
use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\PostRemove; use Doctrine\ORM\Mapping\PostRemove;
use Doctrine\ORM\Mapping\PreUpdate; use Doctrine\ORM\Mapping\PreUpdate;
use SplFileInfo; use SplFileInfo;
@ -96,6 +97,25 @@ class AttachmentDeleteListener
} }
} }
/**
* Ensure that attachments are not used in preview, so that they can be deleted (without integrity violation).
* @ORM\PreRemove()
*/
public function preRemoveHandler(Attachment $attachment, LifecycleEventArgs $event): void
{
//Ensure that the attachment that will be deleted, is not used as preview picture anymore...
$attachment_holder = $attachment->getElement();
if ($attachment_holder === null) {
return;
}
//... Otherwise remove it as preview picture
if ($attachment_holder->getMasterPictureAttachment() === $attachment) {
$attachment_holder->setMasterPictureAttachment(null);
}
}
/** /**
* Removes the file associated with the attachment, after the attachment was deleted. * Removes the file associated with the attachment, after the attachment was deleted.
* *