fix(custom-fields) : empêche EntityNotFoundException sur CustomField orphelin
Auto Tag Develop / tag (push) Successful in 9s

Même pattern que la fix Piece (003e419) : helper ensureCustomFieldExists()
qui force l'init du proxy lazy et catch EntityNotFoundException dans
MachineStructureController::normalizeCustomFieldValues() et
CustomFieldValueController::normalizeCustomFieldValue(). Les CFV pointant
vers un CustomField supprimé sont silencieusement skippés au lieu de
crasher la vue machine entière.
This commit is contained in:
Matthieu
2026-05-28 16:48:58 +02:00
parent 7e2cabfa65
commit af13dc0237
2 changed files with 45 additions and 5 deletions
+22 -4
View File
@@ -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 private function normalizeCustomFieldValue(CustomFieldValue $value): array
{ {
$customField = $value->getCustomField(); $customField = $this->ensureCustomFieldExists($value->getCustomField());
return [ return [
'id' => $value->getId(), 'id' => $value->getId(),
'value' => $value->getValue(), 'value' => $value->getValue(),
'customFieldId' => $customField->getId(), 'customFieldId' => $customField?->getId(),
'customField' => [ 'customField' => $customField ? [
'id' => $customField->getId(), 'id' => $customField->getId(),
'name' => $customField->getName(), 'name' => $customField->getName(),
'type' => $customField->getType(), 'type' => $customField->getType(),
'required' => $customField->isRequired(), 'required' => $customField->isRequired(),
'options' => $customField->getOptions(), 'options' => $customField->getOptions(),
'orderIndex' => $customField->getOrderIndex(), 'orderIndex' => $customField->getOrderIndex(),
], ] : null,
'machineId' => $value->getMachine()?->getId(), 'machineId' => $value->getMachine()?->getId(),
'composantId' => $value->getComposant()?->getId(), 'composantId' => $value->getComposant()?->getId(),
'pieceId' => $value->getPiece()?->getId(), 'pieceId' => $value->getPiece()?->getId(),
+23 -1
View File
@@ -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 private function normalizePiece(Piece $piece): array
{ {
$type = $piece->getTypePiece(); $type = $piece->getTypePiece();
@@ -942,7 +961,10 @@ class MachineStructureController extends AbstractController
if (!$cfv instanceof CustomFieldValue) { if (!$cfv instanceof CustomFieldValue) {
continue; continue;
} }
$cf = $cfv->getCustomField(); $cf = $this->ensureCustomFieldExists($cfv->getCustomField());
if (null === $cf) {
continue;
}
$items[] = [ $items[] = [
'id' => $cfv->getId(), 'id' => $cfv->getId(),
'value' => $cfv->getValue(), 'value' => $cfv->getValue(),