From 57642c3f688e7166f26ab6ec7ef9d687a7f464c4 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 5 Feb 2024 01:32:17 -0500 Subject: [PATCH] Internal: Refactor relation between CAnnouncement and CAnnouncementAttachment --- src/CourseBundle/Entity/CAnnouncement.php | 29 ++++++++++++++----- .../Entity/CAnnouncementAttachment.php | 8 ++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/CourseBundle/Entity/CAnnouncement.php b/src/CourseBundle/Entity/CAnnouncement.php index 46df6290e9..94a1eb2136 100644 --- a/src/CourseBundle/Entity/CAnnouncement.php +++ b/src/CourseBundle/Entity/CAnnouncement.php @@ -38,11 +38,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface, Strin #[ORM\Column(name: 'email_sent', type: 'boolean', nullable: true)] protected ?bool $emailSent = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: CAnnouncementAttachment::class, mappedBy: 'announcement', cascade: ['persist', 'remove'], orphanRemoval: true)] - protected Collection $attachments; + #[ORM\OneToMany(mappedBy: 'announcement', targetEntity: CAnnouncementAttachment::class, cascade: ['persist'])] + private Collection $attachments; public function __construct() { @@ -55,14 +52,32 @@ class CAnnouncement extends AbstractResource implements ResourceInterface, Strin return $this->getTitle(); } + /** + * @return Collection + */ public function getAttachments(): Collection { return $this->attachments; } - public function setAttachments(Collection $attachments): self + public function addAttachment(CAnnouncementAttachment $attachment): static + { + if (!$this->attachments->contains($attachment)) { + $this->attachments->add($attachment); + $attachment->setAnnouncement($this); + } + + return $this; + } + + public function removeAttachment(CAnnouncementAttachment $attachment): static { - $this->attachments = $attachments; + if ($this->attachments->removeElement($attachment)) { + // set the owning side to null (unless already changed) + if ($attachment->getAnnouncement() === $this) { + $attachment->setAnnouncement(null); + } + } return $this; } diff --git a/src/CourseBundle/Entity/CAnnouncementAttachment.php b/src/CourseBundle/Entity/CAnnouncementAttachment.php index ead9de1f93..7ef5cf4294 100644 --- a/src/CourseBundle/Entity/CAnnouncementAttachment.php +++ b/src/CourseBundle/Entity/CAnnouncementAttachment.php @@ -33,9 +33,9 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf #[ORM\Column(name: 'size', type: 'integer', nullable: false)] protected int $size; - #[ORM\ManyToOne(targetEntity: CAnnouncement::class, inversedBy: 'attachments', cascade: ['persist'])] + #[ORM\ManyToOne(targetEntity: CAnnouncement::class, inversedBy: 'attachments')] #[ORM\JoinColumn(name: 'announcement_id', referencedColumnName: 'iid', onDelete: 'CASCADE')] - protected CAnnouncement $announcement; + private ?CAnnouncement $announcement = null; #[ORM\Column(name: 'filename', type: 'string', length: 255, nullable: false)] protected string $filename; @@ -123,12 +123,12 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf return $this->filename; } - public function getAnnouncement(): CAnnouncement + public function getAnnouncement(): ?CAnnouncement { return $this->announcement; } - public function setAnnouncement(CAnnouncement $announcement): self + public function setAnnouncement(?CAnnouncement $announcement): static { $this->announcement = $announcement;