From 8f5cd98b82d11cfb2c34f91373899b157803f0d9 Mon Sep 17 00:00:00 2001 From: r-dev Date: Sun, 3 May 2026 19:59:03 +0200 Subject: [PATCH] fix(machine-clone) : preserve context field values when cloning a machine Context CustomFieldValues attached to component/piece links were silently dropped from the clone response (and from any subsequent read in the same request) because the controller persisted the new CFVs without adding them to the inverse-side collection of the new link. Doctrine does not auto-sync inverse OneToMany associations, so getContextFieldValues() returned an empty collection on the freshly persisted link. Also synchronise the inverse collection in the test factory so identity-mapped entities reflect newly-created CFVs when reused by request handlers within the same test. Co-Authored-By: RuFlo --- src/Controller/MachineStructureController.php | 2 ++ tests/AbstractApiTestCase.php | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Controller/MachineStructureController.php b/src/Controller/MachineStructureController.php index 10f753d..7331a55 100644 --- a/src/Controller/MachineStructureController.php +++ b/src/Controller/MachineStructureController.php @@ -337,6 +337,7 @@ class MachineStructureController extends AbstractController $newValue->setMachineComponentLink($newLink); $newValue->setComposant($newLink->getComposant()); $this->entityManager->persist($newValue); + $newLink->getContextFieldValues()->add($newValue); } } @@ -352,6 +353,7 @@ class MachineStructureController extends AbstractController $newValue->setMachinePieceLink($newLink); $newValue->setPiece($newLink->getPiece()); $this->entityManager->persist($newValue); + $newLink->getContextFieldValues()->add($newValue); } } } diff --git a/tests/AbstractApiTestCase.php b/tests/AbstractApiTestCase.php index 8ad92b5..a0d8012 100644 --- a/tests/AbstractApiTestCase.php +++ b/tests/AbstractApiTestCase.php @@ -7,10 +7,10 @@ namespace App\Tests; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Symfony\Bundle\Test\Client; use App\Entity\Composant; +use App\Entity\ComposantConstructeurLink; use App\Entity\ComposantPieceSlot; use App\Entity\ComposantProductSlot; use App\Entity\ComposantSubcomponentSlot; -use App\Entity\ComposantConstructeurLink; use App\Entity\Constructeur; use App\Entity\CustomField; use App\Entity\CustomFieldValue; @@ -467,6 +467,14 @@ abstract class AbstractApiTestCase extends ApiTestCase $em->persist($cfv); $em->flush(); + // Keep inverse-side collections in sync so identity-mapped entities reflect the new CFV. + if (null !== $machineComponentLink && !$machineComponentLink->getContextFieldValues()->contains($cfv)) { + $machineComponentLink->getContextFieldValues()->add($cfv); + } + if (null !== $machinePieceLink && !$machinePieceLink->getContextFieldValues()->contains($cfv)) { + $machinePieceLink->getContextFieldValues()->add($cfv); + } + return $cfv; }