From c46769a67d36aa661b0f0dd8badea99c9df7dd42 Mon Sep 17 00:00:00 2001 From: r-dev Date: Sun, 3 May 2026 19:29:36 +0200 Subject: [PATCH] fix(model-types) : nullify weak references on ModelType delete Belt-and-suspenders against orphan refs when a ModelType is deleted: applicatively nullifies typeComposantId / typePieceId / typeProductId on every "ON DELETE SET NULL" relationship before the row is removed, in case the database FK cascade fails to fire. Observed in prod 2026-04-28: deletion of ModelType "Paliers" left an orphan in skeleton_subcomponent_requirements, surfacing as a 500 when API Platform tried to lazy-load the missing proxy. Co-Authored-By: RuFlo --- .../ModelTypeReferenceCleanupSubscriber.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/EventSubscriber/ModelTypeReferenceCleanupSubscriber.php diff --git a/src/EventSubscriber/ModelTypeReferenceCleanupSubscriber.php b/src/EventSubscriber/ModelTypeReferenceCleanupSubscriber.php new file mode 100644 index 0000000..52bae91 --- /dev/null +++ b/src/EventSubscriber/ModelTypeReferenceCleanupSubscriber.php @@ -0,0 +1,57 @@ + */ + private const NULLABLE_REFERENCES = [ + ['skeleton_subcomponent_requirements', 'typecomposantid'], + ['skeleton_piece_requirements', 'typepieceid'], + ['skeleton_product_requirements', 'typeproductid'], + ['composant_piece_slots', 'typepieceid'], + ['composant_product_slots', 'typeproductid'], + ['composant_subcomponent_slots', 'typecomposantid'], + ['piece_product_slots', 'typeproductid'], + ['machine_component_links', 'modeltypeid'], + ['machine_piece_links', 'modeltypeid'], + ['machine_product_links', 'modeltypeid'], + ]; + + public function preRemove(PreRemoveEventArgs $args): void + { + $entity = $args->getObject(); + if (!$entity instanceof ModelType) { + return; + } + + $id = $entity->getId(); + if (!$id) { + return; + } + + $conn = $args->getObjectManager()->getConnection(); + foreach (self::NULLABLE_REFERENCES as [$table, $column]) { + $conn->executeStatement( + sprintf('UPDATE %s SET %s = NULL WHERE %s = ?', $table, $column, $column), + [$id], + ); + } + } +}