diff --git a/src/Controller/CustomFieldValueController.php b/src/Controller/CustomFieldValueController.php index 34ea823..94f8d22 100644 --- a/src/Controller/CustomFieldValueController.php +++ b/src/Controller/CustomFieldValueController.php @@ -315,22 +315,40 @@ class CustomFieldValueController extends AbstractController } } + /** + * Same as ensurePieceExists() but for CustomField, to keep orphan CFVs from + * crashing the value endpoints. + */ + private function ensureCustomFieldExists(?CustomField $cf): ?CustomField + { + if (null === $cf) { + return null; + } + try { + $cf->getId(); + + return $cf; + } catch (EntityNotFoundException) { + return null; + } + } + private function normalizeCustomFieldValue(CustomFieldValue $value): array { - $customField = $value->getCustomField(); + $customField = $this->ensureCustomFieldExists($value->getCustomField()); return [ 'id' => $value->getId(), 'value' => $value->getValue(), - 'customFieldId' => $customField->getId(), - 'customField' => [ + 'customFieldId' => $customField?->getId(), + 'customField' => $customField ? [ 'id' => $customField->getId(), 'name' => $customField->getName(), 'type' => $customField->getType(), 'required' => $customField->isRequired(), 'options' => $customField->getOptions(), 'orderIndex' => $customField->getOrderIndex(), - ], + ] : null, 'machineId' => $value->getMachine()?->getId(), 'composantId' => $value->getComposant()?->getId(), 'pieceId' => $value->getPiece()?->getId(), diff --git a/src/Controller/MachineStructureController.php b/src/Controller/MachineStructureController.php index 602ac9d..ab5ae9b 100644 --- a/src/Controller/MachineStructureController.php +++ b/src/Controller/MachineStructureController.php @@ -832,6 +832,25 @@ class MachineStructureController extends AbstractController } } + /** + * Same defensive pattern as ensurePieceExists() but for CustomField. + * A CustomFieldValue may carry a stale FK to a deleted CustomField; touching + * the lazy proxy raises EntityNotFoundException and 500s the machine view. + */ + private function ensureCustomFieldExists(?CustomField $cf): ?CustomField + { + if (null === $cf) { + return null; + } + try { + $cf->getId(); + + return $cf; + } catch (EntityNotFoundException) { + return null; + } + } + private function normalizePiece(Piece $piece): array { $type = $piece->getTypePiece(); @@ -942,7 +961,10 @@ class MachineStructureController extends AbstractController if (!$cfv instanceof CustomFieldValue) { continue; } - $cf = $cfv->getCustomField(); + $cf = $this->ensureCustomFieldExists($cfv->getCustomField()); + if (null === $cf) { + continue; + } $items[] = [ 'id' => $cfv->getId(), 'value' => $cfv->getValue(),