From 1c4ee375077ec4cd522872393baf6123bb992121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 7 Apr 2025 14:35:07 +0200 Subject: [PATCH] Refactor entity normalization to handle Doctrine Collection. Updated `normalizeDoctrineEntity` to support normalizing `Collection` objects and handle null values in arrays. This ensures compatibility with a broader range of entity structures and improves data integrity during normalization. --- .../ChillMainBundle/Export/ExportDataNormalizerTrait.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php b/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php index 9dafd597b..44cdc8310 100644 --- a/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php +++ b/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Export; +use Doctrine\Common\Collections\Collection; use Doctrine\Persistence\ObjectRepository; trait ExportDataNormalizerTrait @@ -21,10 +22,13 @@ trait ExportDataNormalizerTrait public function normalizeDoctrineEntity(object|array $entity): array|int { if (is_array($entity)) { - return array_values(array_map(static fn (object $entity) => $entity->getId(), $entity)); + return array_values(array_filter(array_map(static fn (object $entity) => $entity->getId(), $entity), fn ($value) => null !== $value)); + } + if ($entity instanceof Collection) { + return $this->normalizeDoctrineEntity($entity->toArray()); } - return $entity->getId(); + return $entity?->getId(); } public function denormalizeDoctrineEntity(array|int $id, ObjectRepository $repository): object|array