. */ declare(strict_types=1); namespace App\Entity\Parts\PartTraits; use App\Entity\Parts\Part; use App\Entity\Parts\PartAssociation; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Symfony\Component\Validator\Constraints\Valid; use Doctrine\ORM\Mapping as ORM; trait AssociationTrait { /** * @var Collection All associations where this part is the owner */ #[Valid] #[ORM\OneToMany(mappedBy: 'owner', targetEntity: PartAssociation::class, cascade: ['persist', 'remove'], orphanRemoval: true)] protected Collection $associated_parts_as_owner; /** * @var Collection All associations where this part is the owned/other part */ #[Valid] #[ORM\OneToMany(mappedBy: 'other', targetEntity: PartAssociation::class, cascade: ['persist', 'remove'], orphanRemoval: true)] protected Collection $associated_parts_as_other; /** * Returns all associations where this part is the owner. * @return Collection */ public function getAssociatedPartsAsOwner(): Collection { return $this->associated_parts_as_owner; } /** * Add a new association where this part is the owner. * @param PartAssociation $association * @return $this */ public function addAssociatedPartsAsOwner(PartAssociation $association): self { //Ensure that the association is really owned by this part $association->setOwner($this); $this->associated_parts_as_owner->add($association); return $this; } /** * Remove an association where this part is the owner. * @param PartAssociation $association * @return $this */ public function removeAssociatedPartsAsOwner(PartAssociation $association): self { $this->associated_parts_as_owner->removeElement($association); return $this; } /** * Returns all associations where this part is the owned/other part. * If you want to modify the association, do it on the owning part * @return Collection */ public function getAssociatedPartsAsOther(): Collection { return $this->associated_parts_as_other; } /** * Returns all associations where this part is the owned or other part. * @return Collection */ public function getAssociatedPartsAll(): Collection { return new ArrayCollection( array_merge( $this->associated_parts_as_owner->toArray(), $this->associated_parts_as_other->toArray() ) ); } }