Internal: Refactor relation between CAnnouncement and CAnnouncementAttachment

pull/5140/head^2
Angel Fernando Quiroz Campos 2 years ago
parent 54e454b168
commit 57642c3f68
  1. 29
      src/CourseBundle/Entity/CAnnouncement.php
  2. 8
      src/CourseBundle/Entity/CAnnouncementAttachment.php

@ -38,11 +38,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface, Strin
#[ORM\Column(name: 'email_sent', type: 'boolean', nullable: true)] #[ORM\Column(name: 'email_sent', type: 'boolean', nullable: true)]
protected ?bool $emailSent = null; protected ?bool $emailSent = null;
/** #[ORM\OneToMany(mappedBy: 'announcement', targetEntity: CAnnouncementAttachment::class, cascade: ['persist'])]
* @var Collection<int, CAnnouncementAttachment> private Collection $attachments;
*/
#[ORM\OneToMany(targetEntity: CAnnouncementAttachment::class, mappedBy: 'announcement', cascade: ['persist', 'remove'], orphanRemoval: true)]
protected Collection $attachments;
public function __construct() public function __construct()
{ {
@ -55,14 +52,32 @@ class CAnnouncement extends AbstractResource implements ResourceInterface, Strin
return $this->getTitle(); return $this->getTitle();
} }
/**
* @return Collection<int, CAnnouncementAttachment>
*/
public function getAttachments(): Collection public function getAttachments(): Collection
{ {
return $this->attachments; 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; return $this;
} }

@ -33,9 +33,9 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf
#[ORM\Column(name: 'size', type: 'integer', nullable: false)] #[ORM\Column(name: 'size', type: 'integer', nullable: false)]
protected int $size; 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')] #[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)] #[ORM\Column(name: 'filename', type: 'string', length: 255, nullable: false)]
protected string $filename; protected string $filename;
@ -123,12 +123,12 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf
return $this->filename; return $this->filename;
} }
public function getAnnouncement(): CAnnouncement public function getAnnouncement(): ?CAnnouncement
{ {
return $this->announcement; return $this->announcement;
} }
public function setAnnouncement(CAnnouncement $announcement): self public function setAnnouncement(?CAnnouncement $announcement): static
{ {
$this->announcement = $announcement; $this->announcement = $announcement;

Loading…
Cancel
Save