From aec33e791164309cae5e3b0d3d4d9f10b2e394f5 Mon Sep 17 00:00:00 2001 From: r-dev Date: Fri, 3 Apr 2026 10:52:50 +0200 Subject: [PATCH] feat(custom-fields) : clone context field values on machine clone Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Controller/MachineStructureController.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Controller/MachineStructureController.php b/src/Controller/MachineStructureController.php index f9fdd4a..adc4b4f 100644 --- a/src/Controller/MachineStructureController.php +++ b/src/Controller/MachineStructureController.php @@ -162,6 +162,9 @@ class MachineStructureController extends AbstractController // Copy product links $this->cloneProductLinks($source, $newMachine, $componentLinkMap, $pieceLinkMap); + // Copy context field values + $this->cloneContextFieldValues($componentLinkMap, $pieceLinkMap); + $this->entityManager->flush(); $componentLinks = $this->machineComponentLinkRepository->findBy(['machine' => $newMachine], ['createdAt' => 'ASC']); @@ -313,6 +316,45 @@ class MachineStructureController extends AbstractController } } + /** + * @param array $componentLinkMap + * @param array $pieceLinkMap + */ + private function cloneContextFieldValues( + array $componentLinkMap, + array $pieceLinkMap, + ): void { + foreach ($componentLinkMap as $oldLinkId => $newLink) { + $oldLink = $this->machineComponentLinkRepository->find($oldLinkId); + if (!$oldLink) { + continue; + } + foreach ($oldLink->getContextFieldValues() as $cfv) { + $newValue = new CustomFieldValue(); + $newValue->setCustomField($cfv->getCustomField()); + $newValue->setValue($cfv->getValue()); + $newValue->setMachineComponentLink($newLink); + $newValue->setComposant($newLink->getComposant()); + $this->entityManager->persist($newValue); + } + } + + foreach ($pieceLinkMap as $oldLinkId => $newLink) { + $oldLink = $this->machinePieceLinkRepository->find($oldLinkId); + if (!$oldLink) { + continue; + } + foreach ($oldLink->getContextFieldValues() as $cfv) { + $newValue = new CustomFieldValue(); + $newValue->setCustomField($cfv->getCustomField()); + $newValue->setValue($cfv->getValue()); + $newValue->setMachinePieceLink($newLink); + $newValue->setPiece($newLink->getPiece()); + $this->entityManager->persist($newValue); + } + } + } + private function normalizePayloadList(mixed $value): array { if (!is_array($value)) {