diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index fa01d212..9f7cda95 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -41,6 +41,7 @@ use App\ApiPlatform\Filter\EntityFilter; use App\ApiPlatform\Filter\LikeFilter; use App\ApiPlatform\Filter\PartStoragelocationFilter; use App\Entity\Attachments\AttachmentTypeAttachment; +use App\Entity\Parts\PartTraits\AssociationTrait; use App\Repository\PartRepository; use Doctrine\DBAL\Types\Types; use App\Entity\Attachments\Attachment; @@ -58,6 +59,7 @@ use DateTime; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Jfcherng\Diff\Utility\Arr; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints as Assert; @@ -112,6 +114,7 @@ class Part extends AttachmentContainingDBElement use OrderTrait; use ParametersTrait; use ProjectTrait; + use AssociationTrait; /** @var Collection */ @@ -165,6 +168,9 @@ class Part extends AttachmentContainingDBElement $this->parameters = new ArrayCollection(); $this->project_bom_entries = new ArrayCollection(); + $this->associated_parts_as_owner = new ArrayCollection(); + $this->associated_parts_as_other = new ArrayCollection(); + //By default, the part has no provider $this->providerReference = InfoProviderReference::noProvider(); } @@ -193,6 +199,13 @@ class Part extends AttachmentContainingDBElement $this->addParameter(clone $parameter); } + //Deep clone the owned part associations (the owned ones make not much sense without the owner) + $ownedAssociations = $this->associated_parts_as_owner; + $this->associated_parts_as_owner = new ArrayCollection(); + foreach ($ownedAssociations as $association) { + $this->addAssociatedPartsAsOwner(clone $association); + } + //Deep clone info provider $this->providerReference = clone $this->providerReference; } diff --git a/src/Entity/Parts/PartAssociation.php b/src/Entity/Parts/PartAssociation.php new file mode 100644 index 00000000..1fc0530d --- /dev/null +++ b/src/Entity/Parts/PartAssociation.php @@ -0,0 +1,117 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Entity\Parts; + +use App\Repository\DBElementRepository; +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Mapping as ORM; +use App\Entity\Base\AbstractDBElement; +use App\Entity\Base\TimestampTrait; +use Symfony\Component\Validator\Constraints as Assert; + +/** + * This entity describes a part association, which is a semantic connection between two parts. + * For example, a part association can be used to describe that a part is a replacement for another part. + */ +#[ORM\Entity(repositoryClass: DBElementRepository::class)] +#[ORM\HasLifecycleCallbacks] +class PartAssociation extends AbstractDBElement +{ + use TimestampTrait; + + /** + * @var PartAssociationType The type of this association (how the two parts are related) + */ + #[ORM\Column(type: Types::SMALLINT, enumType: PartAssociationType::class)] + protected PartAssociationType $type = PartAssociationType::OTHER; + + /** + * @var string|null A comment describing this association further. Can also be used to specify the OTHER type + * further. + */ + #[ORM\Column(type: Types::TEXT, nullable: true)] + protected ?string $comment = null; + + /** + * @var Part|null The part which "owns" this association, e.g. the part which is a replacement for another part + */ + #[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'associated_parts_as_owner')] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] + #[Assert\NotNull] + protected ?Part $owner = null; + + /** + * @var Part|null The part which is "owned" by this association, e.g. the part which is replaced by another part + */ + #[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'associated_parts_as_other')] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] + #[Assert\NotNull] + protected ?Part $other = null; + + public function getType(): PartAssociationType + { + return $this->type; + } + + public function setType(PartAssociationType $type): PartAssociation + { + $this->type = $type; + return $this; + } + + public function getComment(): ?string + { + return $this->comment; + } + + public function setComment(?string $comment): PartAssociation + { + $this->comment = $comment; + return $this; + } + + public function getOwner(): ?Part + { + return $this->owner; + } + + public function setOwner(?Part $owner): PartAssociation + { + $this->owner = $owner; + return $this; + } + + public function getOther(): ?Part + { + return $this->other; + } + + public function setOther(?Part $other): PartAssociation + { + $this->other = $other; + return $this; + } + + +} \ No newline at end of file diff --git a/src/Entity/Parts/PartAssociationType.php b/src/Entity/Parts/PartAssociationType.php new file mode 100644 index 00000000..d9320288 --- /dev/null +++ b/src/Entity/Parts/PartAssociationType.php @@ -0,0 +1,37 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Entity\Parts; + +/** + * The values of this enums are used to describe how two parts are associated with each other. + */ +enum PartAssociationType: int +{ + /** A user definable association type, which can be described in the comment field */ + case OTHER = 0; + /** The owning part is compatible with the other part */ + case COMPATIBLE = 1; + /** The owning part supersedes the other part (owner is newer version) */ + case SUPERSEDES = 2; +} diff --git a/src/Entity/Parts/PartTraits/AssociationTrait.php b/src/Entity/Parts/PartTraits/AssociationTrait.php new file mode 100644 index 00000000..82c46f27 --- /dev/null +++ b/src/Entity/Parts/PartTraits/AssociationTrait.php @@ -0,0 +1,92 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Entity\Parts\PartTraits; + +use App\Entity\Parts\PartAssociation; +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; + } +} \ No newline at end of file