From 8871440c9a8e7b4958aa1d8b2446fe2b592a9972 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Fri, 13 Mar 2026 11:19:42 +0100 Subject: [PATCH] fix(custom-fields) : populate customFields in ModelType structure from relations getStructure() was returning hardcoded empty customFields arrays after the JSON-to-tables migration. Now reads from the relational CustomField entities via serializeCustomFields() for all three categories. Co-Authored-By: Claude Opus 4.6 --- src/Entity/ModelType.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Entity/ModelType.php b/src/Entity/ModelType.php index c42458f..7f68799 100644 --- a/src/Entity/ModelType.php +++ b/src/Entity/ModelType.php @@ -221,7 +221,7 @@ class ModelType return match ($this->category) { ModelCategory::COMPONENT => $this->getComponentStructureFromRelations(), ModelCategory::PIECE => $this->getPieceStructureFromRelations(), - ModelCategory::PRODUCT => ['customFields' => []], + ModelCategory::PRODUCT => ['customFields' => $this->serializeCustomFields($this->productCustomFields)], }; } @@ -344,7 +344,7 @@ class ModelType private function getComponentStructureFromRelations(): array { - $structure = ['customFields' => [], 'pieces' => [], 'products' => [], 'subcomponents' => []]; + $structure = ['customFields' => $this->serializeCustomFields($this->customFields), 'pieces' => [], 'products' => [], 'subcomponents' => []]; foreach ($this->skeletonPieceRequirements as $req) { $structure['pieces'][] = [ @@ -373,11 +373,34 @@ class ModelType private function getPieceStructureFromRelations(): array { return [ - 'customFields' => [], + 'customFields' => $this->serializeCustomFields($this->pieceCustomFields), 'products' => array_map(fn (SkeletonProductRequirement $req) => [ 'typeProductId' => $req->getTypeProduct()->getId(), 'familyCode' => $req->getFamilyCode(), ], $this->skeletonProductRequirements->toArray()), ]; } + + /** + * @param Collection $fields + */ + private function serializeCustomFields(Collection $fields): array + { + $items = []; + foreach ($fields as $cf) { + $items[] = [ + 'id' => $cf->getId(), + 'name' => $cf->getName(), + 'type' => $cf->getType(), + 'required' => $cf->isRequired(), + 'options' => $cf->getOptions(), + 'defaultValue' => $cf->getDefaultValue(), + 'orderIndex' => $cf->getOrderIndex(), + ]; + } + + usort($items, static fn (array $a, array $b) => $a['orderIndex'] <=> $b['orderIndex']); + + return $items; + } }