diff --git a/migrations/Version20260403084805.php b/migrations/Version20260403084805.php new file mode 100644 index 0000000..c10c5bf --- /dev/null +++ b/migrations/Version20260403084805.php @@ -0,0 +1,56 @@ +addSql('ALTER TABLE custom_fields ADD COLUMN IF NOT EXISTS machinecontextonly BOOLEAN DEFAULT false NOT NULL'); + + $this->addSql('ALTER TABLE custom_field_values ADD COLUMN IF NOT EXISTS machinecomponentlinkid VARCHAR(36) DEFAULT NULL'); + $this->addSql('ALTER TABLE custom_field_values ADD COLUMN IF NOT EXISTS machinepiecelinkid VARCHAR(36) DEFAULT NULL'); + + $this->addSql(<<<'SQL' + DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_cfv_machine_component_link') THEN + ALTER TABLE custom_field_values ADD CONSTRAINT fk_cfv_machine_component_link + FOREIGN KEY (machinecomponentlinkid) REFERENCES machine_component_links(id) ON DELETE CASCADE; + END IF; + END $$; + SQL); + + $this->addSql(<<<'SQL' + DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_cfv_machine_piece_link') THEN + ALTER TABLE custom_field_values ADD CONSTRAINT fk_cfv_machine_piece_link + FOREIGN KEY (machinepiecelinkid) REFERENCES machine_piece_links(id) ON DELETE CASCADE; + END IF; + END $$; + SQL); + + $this->addSql('CREATE INDEX IF NOT EXISTS idx_cfv_machine_component_link ON custom_field_values(machinecomponentlinkid)'); + $this->addSql('CREATE INDEX IF NOT EXISTS idx_cfv_machine_piece_link ON custom_field_values(machinepiecelinkid)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE custom_field_values DROP CONSTRAINT IF EXISTS fk_cfv_machine_component_link'); + $this->addSql('ALTER TABLE custom_field_values DROP CONSTRAINT IF EXISTS fk_cfv_machine_piece_link'); + $this->addSql('DROP INDEX IF EXISTS idx_cfv_machine_component_link'); + $this->addSql('DROP INDEX IF EXISTS idx_cfv_machine_piece_link'); + $this->addSql('ALTER TABLE custom_field_values DROP COLUMN IF EXISTS machinecomponentlinkid'); + $this->addSql('ALTER TABLE custom_field_values DROP COLUMN IF EXISTS machinepiecelinkid'); + $this->addSql('ALTER TABLE custom_fields DROP COLUMN IF EXISTS machinecontextonly'); + } +} diff --git a/src/Entity/CustomField.php b/src/Entity/CustomField.php index ff82b54..aab34d2 100644 --- a/src/Entity/CustomField.php +++ b/src/Entity/CustomField.php @@ -55,6 +55,10 @@ class CustomField #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private bool $required = false; + #[ORM\Column(type: Types::BOOLEAN, options: ['default' => false], name: 'machinecontextonly')] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] + private bool $machineContextOnly = false; + #[ORM\Column(type: Types::STRING, length: 255, nullable: true, name: 'defaultValue')] private ?string $defaultValue = null; @@ -220,4 +224,16 @@ class CustomField return $this; } + + public function isMachineContextOnly(): bool + { + return $this->machineContextOnly; + } + + public function setMachineContextOnly(bool $machineContextOnly): static + { + $this->machineContextOnly = $machineContextOnly; + + return $this; + } } diff --git a/src/Entity/CustomFieldValue.php b/src/Entity/CustomFieldValue.php index 507d028..99064f9 100644 --- a/src/Entity/CustomFieldValue.php +++ b/src/Entity/CustomFieldValue.php @@ -66,6 +66,14 @@ class CustomFieldValue #[ORM\JoinColumn(name: 'productId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] private ?Product $product = null; + #[ORM\ManyToOne(targetEntity: MachineComponentLink::class, inversedBy: 'contextFieldValues')] + #[ORM\JoinColumn(name: 'machinecomponentlinkid', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] + private ?MachineComponentLink $machineComponentLink = null; + + #[ORM\ManyToOne(targetEntity: MachinePieceLink::class, inversedBy: 'contextFieldValues')] + #[ORM\JoinColumn(name: 'machinepiecelinkid', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] + private ?MachinePieceLink $machinePieceLink = null; + #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')] #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private DateTimeImmutable $createdAt; @@ -151,4 +159,28 @@ class CustomFieldValue return $this; } + + public function getMachineComponentLink(): ?MachineComponentLink + { + return $this->machineComponentLink; + } + + public function setMachineComponentLink(?MachineComponentLink $machineComponentLink): static + { + $this->machineComponentLink = $machineComponentLink; + + return $this; + } + + public function getMachinePieceLink(): ?MachinePieceLink + { + return $this->machinePieceLink; + } + + public function setMachinePieceLink(?MachinePieceLink $machinePieceLink): static + { + $this->machinePieceLink = $machinePieceLink; + + return $this; + } } diff --git a/src/Entity/MachineComponentLink.php b/src/Entity/MachineComponentLink.php index e22d223..97266cd 100644 --- a/src/Entity/MachineComponentLink.php +++ b/src/Entity/MachineComponentLink.php @@ -75,6 +75,12 @@ class MachineComponentLink #[ORM\OneToMany(mappedBy: 'parentComponentLink', targetEntity: MachineProductLink::class)] private Collection $productLinks; + /** + * @var Collection + */ + #[ORM\OneToMany(mappedBy: 'machineComponentLink', targetEntity: CustomFieldValue::class, cascade: ['persist', 'remove'], orphanRemoval: true)] + private Collection $contextFieldValues; + #[ORM\Column(type: Types::STRING, length: 255, nullable: true, name: 'nameOverride')] private ?string $nameOverride = null; @@ -92,11 +98,12 @@ class MachineComponentLink public function __construct() { - $this->createdAt = new DateTimeImmutable(); - $this->updatedAt = new DateTimeImmutable(); - $this->childLinks = new ArrayCollection(); - $this->pieceLinks = new ArrayCollection(); - $this->productLinks = new ArrayCollection(); + $this->createdAt = new DateTimeImmutable(); + $this->updatedAt = new DateTimeImmutable(); + $this->childLinks = new ArrayCollection(); + $this->pieceLinks = new ArrayCollection(); + $this->productLinks = new ArrayCollection(); + $this->contextFieldValues = new ArrayCollection(); } public function getMachine(): Machine @@ -182,4 +189,12 @@ class MachineComponentLink return $this; } + + /** + * @return Collection + */ + public function getContextFieldValues(): Collection + { + return $this->contextFieldValues; + } } diff --git a/src/Entity/MachinePieceLink.php b/src/Entity/MachinePieceLink.php index e580d03..4dd4f60 100644 --- a/src/Entity/MachinePieceLink.php +++ b/src/Entity/MachinePieceLink.php @@ -64,6 +64,12 @@ class MachinePieceLink #[ORM\OneToMany(mappedBy: 'parentPieceLink', targetEntity: MachineProductLink::class)] private Collection $productLinks; + /** + * @var Collection + */ + #[ORM\OneToMany(mappedBy: 'machinePieceLink', targetEntity: CustomFieldValue::class, cascade: ['persist', 'remove'], orphanRemoval: true)] + private Collection $contextFieldValues; + #[ORM\Column(type: Types::STRING, length: 255, nullable: true, name: 'nameOverride')] private ?string $nameOverride = null; @@ -85,9 +91,10 @@ class MachinePieceLink public function __construct() { - $this->createdAt = new DateTimeImmutable(); - $this->updatedAt = new DateTimeImmutable(); - $this->productLinks = new ArrayCollection(); + $this->createdAt = new DateTimeImmutable(); + $this->updatedAt = new DateTimeImmutable(); + $this->productLinks = new ArrayCollection(); + $this->contextFieldValues = new ArrayCollection(); } public function getMachine(): Machine @@ -185,4 +192,12 @@ class MachinePieceLink return $this; } + + /** + * @return Collection + */ + public function getContextFieldValues(): Collection + { + return $this->contextFieldValues; + } }