From bb300a7ca7a542bf93737133419d930ff3874d2e Mon Sep 17 00:00:00 2001 From: Matthieu Date: Fri, 13 Mar 2026 11:21:17 +0100 Subject: [PATCH] feat(composant) : add virtual getStructure() rebuilding legacy JSON from slot tables Exposes structure as a computed property from pieceSlots, productSlots, and subcomponentSlots relations, including slotId for frontend quantity persistence. Co-Authored-By: Claude Opus 4.6 --- src/Entity/Composant.php | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/Entity/Composant.php b/src/Entity/Composant.php index 13700f8..5762c80 100644 --- a/src/Entity/Composant.php +++ b/src/Entity/Composant.php @@ -337,6 +337,59 @@ class Composant return $this->productSlots; } + /** + * Virtual property — rebuilds the legacy structure JSON from slot tables. + * + * @return null|array{pieces: list>, products: list>, subcomponents: list>} + */ + #[Groups(['composant:read'])] + public function getStructure(): ?array + { + $pieces = []; + foreach ($this->pieceSlots as $slot) { + $pieces[] = [ + 'slotId' => $slot->getId(), + 'typePieceId' => $slot->getTypePiece()?->getId(), + 'selectedPieceId' => $slot->getSelectedPiece()?->getId(), + 'quantity' => $slot->getQuantity(), + 'position' => $slot->getPosition(), + ]; + } + + $products = []; + foreach ($this->productSlots as $slot) { + $products[] = [ + 'slotId' => $slot->getId(), + 'typeProductId' => $slot->getTypeProduct()?->getId(), + 'selectedProductId' => $slot->getSelectedProduct()?->getId(), + 'familyCode' => $slot->getFamilyCode(), + 'position' => $slot->getPosition(), + ]; + } + + $subcomponents = []; + foreach ($this->subcomponentSlots as $slot) { + $subcomponents[] = [ + 'slotId' => $slot->getId(), + 'alias' => $slot->getAlias(), + 'familyCode' => $slot->getFamilyCode(), + 'typeComposantId' => $slot->getTypeComposant()?->getId(), + 'selectedComponentId' => $slot->getSelectedComposant()?->getId(), + 'position' => $slot->getPosition(), + ]; + } + + if (empty($pieces) && empty($products) && empty($subcomponents)) { + return null; + } + + return [ + 'pieces' => $pieces, + 'products' => $products, + 'subcomponents' => $subcomponents, + ]; + } + public function addProductSlot(ComposantProductSlot $productSlot): static { if (!$this->productSlots->contains($productSlot)) {