From 3003ced157bf2d8e7dbd9b410ac06afed8bda373 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Thu, 28 May 2026 17:15:03 +0200 Subject: [PATCH] =?UTF-8?q?fix(custom-fields)=20:=20prot=C3=A9ger=20les=20?= =?UTF-8?q?flushs=20contre=20les=20CustomField=20orphelins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deux endroits accèdent à $cfv->getCustomField()->getName() à chaque flush touchant un CustomFieldValue. Si la CustomField a été supprimée et que la FK n'est pas en ON DELETE CASCADE, le proxy lève EntityNotFoundException et fait crasher tout le flush (pas juste une lecture, comme dans le crash côté MachineStructureController). - ReferenceAutoGenerator::buildValueMap() : skip le CFV orphelin (la ref auto retombera proprement sur null via le check requiredFields existant). - AbstractAuditSubscriber::trackCustomFieldValueChange() : skip l'entrée d'audit pour ce CFV au lieu de propager l'exception. --- src/EventSubscriber/AbstractAuditSubscriber.php | 8 +++++++- src/Service/ReferenceAutoGenerator.php | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/EventSubscriber/AbstractAuditSubscriber.php b/src/EventSubscriber/AbstractAuditSubscriber.php index a064f1f..8c82902 100644 --- a/src/EventSubscriber/AbstractAuditSubscriber.php +++ b/src/EventSubscriber/AbstractAuditSubscriber.php @@ -18,6 +18,7 @@ use DateTimeInterface; use Doctrine\Common\Collections\Collection; use Doctrine\Common\EventSubscriber; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityNotFoundException; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Events; use Doctrine\ORM\UnitOfWork; @@ -432,7 +433,12 @@ abstract class AbstractAuditSubscriber implements EventSubscriber return; } - $fieldName = 'customField:'.$cfv->getCustomField()->getName(); + try { + $cfName = $cfv->getCustomField()->getName(); + } catch (EntityNotFoundException) { + return; + } + $fieldName = 'customField:'.$cfName; $diff = [$fieldName => ['from' => $from, 'to' => $to]]; $pendingUpdates[$ownerId] = $this->mergeDiffs($pendingUpdates[$ownerId] ?? [], $diff); diff --git a/src/Service/ReferenceAutoGenerator.php b/src/Service/ReferenceAutoGenerator.php index 31a31db..9e57f56 100644 --- a/src/Service/ReferenceAutoGenerator.php +++ b/src/Service/ReferenceAutoGenerator.php @@ -7,6 +7,7 @@ namespace App\Service; use App\Entity\Composant; use App\Entity\CustomFieldValue; use App\Entity\Piece; +use Doctrine\ORM\EntityNotFoundException; class ReferenceAutoGenerator { @@ -48,8 +49,12 @@ class ReferenceAutoGenerator /** @var CustomFieldValue $cfv */ foreach ($entity->getCustomFieldValues() as $cfv) { - $normalized = mb_strtoupper(trim($cfv->getValue())); - $map[$cfv->getCustomField()->getName()] = $normalized; + try { + $name = $cfv->getCustomField()->getName(); + } catch (EntityNotFoundException) { + continue; + } + $map[$name] = mb_strtoupper(trim($cfv->getValue())); } return $map;