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], + ); + } + } +}