record labels. EAGER so the typed * collection is always hydrated (see ServiceItem for the same pitfall). */ #[ORM\ManyToMany(targetEntity: TenantTag::class, fetch: 'EAGER')] #[ORM\JoinTable(name: 'patient_record_tags')] private Collection $tags; #[ORM\OneToMany(targetEntity: PatientSession::class, mappedBy: 'record', cascade: ['remove'])] private Collection $sessions; public function __construct(string $entityType, int $entityId, User $user, string $createdByType, int $createdById) { $this->uuid = Uuid::v4()->toRfc4122(); $this->entityType = $entityType; $this->entityId = $entityId; $this->user = $user; $this->createdByType = $createdByType; $this->createdById = $createdById; $this->createdAt = time(); $this->sessions = new ArrayCollection(); $this->tags = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getUuid(): string { return $this->uuid; } public function getEntityType(): string { return $this->entityType; } public function getEntityId(): int { return $this->entityId; } public function getUser(): User { return $this->user; } public function getCreatedByType(): string { return $this->createdByType; } public function getCreatedById(): int { return $this->createdById; } public function getCreatedAt(): int { return $this->createdAt; } public function getRecordNumber(): ?string { return $this->recordNumber; } public function setRecordNumber(?string $v): self { $this->recordNumber = $v; return $this; } /** @return Collection */ public function getTags(): Collection { // Doctrine hydrates without the constructor; guard the typed property. return $this->tags ??= new ArrayCollection(); } /** @param TenantTag[] $tags */ public function setTags(array $tags): self { $collection = $this->getTags(); $collection->clear(); foreach ($tags as $t) { if (!$collection->contains($t)) { $collection->add($t); } } return $this; } public function toArray(): array { return [ 'uuid' => $this->uuid, 'entity_type' => $this->entityType, 'entity_id' => $this->entityId, 'user_uuid' => $this->user->getUuid(), 'user_name' => $this->user->getRealName(), 'user_mobile' => $this->user->getMobileNumber(), 'user_national_code' => $this->user->getNationalCode(), 'record_number' => $this->recordNumber, 'tags' => array_map( fn(TenantTag $t) => $t->toArray(), array_values($this->getTags()->toArray()) ), 'created_by_type' => $this->createdByType, 'created_at' => $this->createdAt, ]; } }