diff --git a/src/Entity/Composant.php b/src/Entity/Composant.php index 0aeb337..6c62119 100644 --- a/src/Entity/Composant.php +++ b/src/Entity/Composant.php @@ -87,16 +87,11 @@ class Composant private ?Product $product = null; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Constructeur::class, inversedBy: 'composants')] - #[ORM\JoinTable( - name: '_ComposantConstructeurs', - joinColumns: [new ORM\JoinColumn(name: 'A', referencedColumnName: 'id', onDelete: 'CASCADE')], - inverseJoinColumns: [new ORM\InverseJoinColumn(name: 'B', referencedColumnName: 'id', onDelete: 'CASCADE')] - )] + #[ORM\OneToMany(mappedBy: 'composant', targetEntity: ComposantConstructeurLink::class, cascade: ['remove'])] #[Groups(['composant:read'])] - private Collection $constructeurs; + private Collection $constructeurLinks; /** * @var Collection @@ -163,7 +158,7 @@ class Composant { $this->createdAt = new DateTimeImmutable(); $this->updatedAt = new DateTimeImmutable(); - $this->constructeurs = new ArrayCollection(); + $this->constructeurLinks = new ArrayCollection(); $this->documents = new ArrayCollection(); $this->customFieldValues = new ArrayCollection(); $this->machineLinks = new ArrayCollection(); @@ -260,43 +255,11 @@ class Composant } /** - * @return Collection + * @return Collection */ - public function getConstructeurs(): Collection + public function getConstructeurLinks(): Collection { - return $this->constructeurs; - } - - /** - * @param iterable $constructeurs - */ - public function setConstructeurs(iterable $constructeurs): static - { - $this->constructeurs = new ArrayCollection(); - - foreach ($constructeurs as $constructeur) { - if ($constructeur instanceof Constructeur && !$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - } - - return $this; - } - - public function addConstructeur(Constructeur $constructeur): static - { - if (!$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - - return $this; - } - - public function removeConstructeur(Constructeur $constructeur): static - { - $this->constructeurs->removeElement($constructeur); - - return $this; + return $this->constructeurLinks; } /** diff --git a/src/Entity/Constructeur.php b/src/Entity/Constructeur.php index c710496..6b6c29f 100644 --- a/src/Entity/Constructeur.php +++ b/src/Entity/Constructeur.php @@ -63,37 +63,37 @@ class Constructeur private DateTimeImmutable $updatedAt; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Machine::class, mappedBy: 'constructeurs')] - private Collection $machines; + #[ORM\OneToMany(mappedBy: 'constructeur', targetEntity: MachineConstructeurLink::class, cascade: ['remove'])] + private Collection $machineLinks; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Composant::class, mappedBy: 'constructeurs')] - private Collection $composants; + #[ORM\OneToMany(mappedBy: 'constructeur', targetEntity: ComposantConstructeurLink::class, cascade: ['remove'])] + private Collection $composantLinks; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Piece::class, mappedBy: 'constructeurs')] - private Collection $pieces; + #[ORM\OneToMany(mappedBy: 'constructeur', targetEntity: PieceConstructeurLink::class, cascade: ['remove'])] + private Collection $pieceLinks; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'constructeurs')] - private Collection $products; + #[ORM\OneToMany(mappedBy: 'constructeur', targetEntity: ProductConstructeurLink::class, cascade: ['remove'])] + private Collection $productLinks; public function __construct() { - $this->createdAt = new DateTimeImmutable(); - $this->updatedAt = new DateTimeImmutable(); - $this->machines = new ArrayCollection(); - $this->composants = new ArrayCollection(); - $this->pieces = new ArrayCollection(); - $this->products = new ArrayCollection(); + $this->createdAt = new DateTimeImmutable(); + $this->updatedAt = new DateTimeImmutable(); + $this->machineLinks = new ArrayCollection(); + $this->composantLinks = new ArrayCollection(); + $this->pieceLinks = new ArrayCollection(); + $this->productLinks = new ArrayCollection(); } public function getName(): ?string diff --git a/src/Entity/Machine.php b/src/Entity/Machine.php index b9c3e48..1696dc7 100644 --- a/src/Entity/Machine.php +++ b/src/Entity/Machine.php @@ -18,12 +18,14 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Serializer\Attribute\Groups; use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: MachineRepository::class)] #[ORM\Table(name: 'machines')] #[ORM\HasLifecycleCallbacks] +#[UniqueEntity(fields: ['reference'], message: 'Une machine avec cette référence existe déjà.')] #[ApiResource( description: 'Machines industrielles rattachées à un site. Chaque machine possède une structure hiérarchique de composants, pièces et produits, ainsi que des champs personnalisés et des documents.', operations: [ @@ -59,15 +61,10 @@ class Machine private ?Site $site = null; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Constructeur::class, inversedBy: 'machines')] - #[ORM\JoinTable( - name: '_MachineConstructeurs', - joinColumns: [new ORM\JoinColumn(name: 'A', referencedColumnName: 'id', onDelete: 'CASCADE')], - inverseJoinColumns: [new ORM\InverseJoinColumn(name: 'B', referencedColumnName: 'id', onDelete: 'CASCADE')] - )] - private Collection $constructeurs; + #[ORM\OneToMany(mappedBy: 'machine', targetEntity: MachineConstructeurLink::class, cascade: ['remove'])] + private Collection $constructeurLinks; /** * @var Collection @@ -125,7 +122,7 @@ class Machine $now = new DateTimeImmutable(); $this->createdAt = $now; $this->updatedAt = $now; - $this->constructeurs = new ArrayCollection(); + $this->constructeurLinks = new ArrayCollection(); $this->componentLinks = new ArrayCollection(); $this->pieceLinks = new ArrayCollection(); $this->productLinks = new ArrayCollection(); @@ -212,27 +209,11 @@ class Machine } /** - * @return Collection + * @return Collection */ - public function getConstructeurs(): Collection + public function getConstructeurLinks(): Collection { - return $this->constructeurs; - } - - public function addConstructeur(Constructeur $constructeur): static - { - if (!$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - - return $this; - } - - public function removeConstructeur(Constructeur $constructeur): static - { - $this->constructeurs->removeElement($constructeur); - - return $this; + return $this->constructeurLinks; } /** diff --git a/src/Entity/Piece.php b/src/Entity/Piece.php index a22b138..edaeaaf 100644 --- a/src/Entity/Piece.php +++ b/src/Entity/Piece.php @@ -86,16 +86,11 @@ class Piece private ?Product $product = null; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Constructeur::class, inversedBy: 'pieces')] - #[ORM\JoinTable( - name: '_PieceConstructeurs', - joinColumns: [new ORM\JoinColumn(name: 'A', referencedColumnName: 'id', onDelete: 'CASCADE')], - inverseJoinColumns: [new ORM\InverseJoinColumn(name: 'B', referencedColumnName: 'id', onDelete: 'CASCADE')] - )] + #[ORM\OneToMany(mappedBy: 'piece', targetEntity: PieceConstructeurLink::class, cascade: ['remove'])] #[Groups(['piece:read'])] - private Collection $constructeurs; + private Collection $constructeurLinks; /** * @var Collection @@ -151,7 +146,7 @@ class Piece { $this->createdAt = new DateTimeImmutable(); $this->updatedAt = new DateTimeImmutable(); - $this->constructeurs = new ArrayCollection(); + $this->constructeurLinks = new ArrayCollection(); $this->documents = new ArrayCollection(); $this->customFieldValues = new ArrayCollection(); $this->products = new ArrayCollection(); @@ -260,43 +255,11 @@ class Piece } /** - * @return Collection + * @return Collection */ - public function getConstructeurs(): Collection + public function getConstructeurLinks(): Collection { - return $this->constructeurs; - } - - /** - * @param iterable $constructeurs - */ - public function setConstructeurs(iterable $constructeurs): static - { - $this->constructeurs = new ArrayCollection(); - - foreach ($constructeurs as $constructeur) { - if ($constructeur instanceof Constructeur && !$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - } - - return $this; - } - - public function addConstructeur(Constructeur $constructeur): static - { - if (!$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - - return $this; - } - - public function removeConstructeur(Constructeur $constructeur): static - { - $this->constructeurs->removeElement($constructeur); - - return $this; + return $this->constructeurLinks; } /** diff --git a/src/Entity/Product.php b/src/Entity/Product.php index e814c8f..0512d6d 100644 --- a/src/Entity/Product.php +++ b/src/Entity/Product.php @@ -71,16 +71,11 @@ class Product private ?ModelType $typeProduct = null; /** - * @var Collection + * @var Collection */ - #[ORM\ManyToMany(targetEntity: Constructeur::class, inversedBy: 'products')] - #[ORM\JoinTable( - name: '_ProductConstructeurs', - joinColumns: [new ORM\JoinColumn(name: 'A', referencedColumnName: 'id', onDelete: 'CASCADE')], - inverseJoinColumns: [new ORM\InverseJoinColumn(name: 'B', referencedColumnName: 'id', onDelete: 'CASCADE')] - )] + #[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductConstructeurLink::class, cascade: ['remove'])] #[Groups(['product:read'])] - private Collection $constructeurs; + private Collection $constructeurLinks; /** * @var Collection @@ -138,7 +133,7 @@ class Product { $this->createdAt = new DateTimeImmutable(); $this->updatedAt = new DateTimeImmutable(); - $this->constructeurs = new ArrayCollection(); + $this->constructeurLinks = new ArrayCollection(); $this->documents = new ArrayCollection(); $this->customFieldValues = new ArrayCollection(); $this->pieces = new ArrayCollection(); @@ -196,43 +191,11 @@ class Product } /** - * @return Collection + * @return Collection */ - public function getConstructeurs(): Collection + public function getConstructeurLinks(): Collection { - return $this->constructeurs; - } - - /** - * @param iterable $constructeurs - */ - public function setConstructeurs(iterable $constructeurs): static - { - $this->constructeurs = new ArrayCollection(); - - foreach ($constructeurs as $constructeur) { - if ($constructeur instanceof Constructeur && !$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - } - - return $this; - } - - public function addConstructeur(Constructeur $constructeur): static - { - if (!$this->constructeurs->contains($constructeur)) { - $this->constructeurs->add($constructeur); - } - - return $this; - } - - public function removeConstructeur(Constructeur $constructeur): static - { - $this->constructeurs->removeElement($constructeur); - - return $this; + return $this->constructeurLinks; } /**