Skip to content

Commit 7e49091

Browse files
committed
WIP
1 parent 1ec9644 commit 7e49091

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/Denormalizer/DefaultResourceDenormalizer.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,35 @@ public function denormalize(array $data, string $resourceClass): object
6767
}
6868
}
6969

70-
// Create entity manually and set properties to avoid serializer issues
71-
$entity = new $resourceClass();
72-
73-
foreach ($processedData as $property => $value) {
74-
$setter = 'set' . ucfirst($property);
75-
if (method_exists($entity, $setter)) {
76-
$entity->$setter($value);
70+
// Ensure all entity relationships are properly managed before denormalization
71+
foreach ($processedData as $field => $value) {
72+
if ($metadata->hasAssociation($field)) {
73+
if ($metadata->isCollectionValuedAssociation($field) && is_array($value)) {
74+
foreach ($value as $entity) {
75+
if (is_object($entity) && !$this->entityManager->contains($entity)) {
76+
throw new \RuntimeException(sprintf(
77+
'Entity %s in collection %s is not managed by EntityManager',
78+
get_class($entity),
79+
$field
80+
));
81+
}
82+
}
83+
} elseif (is_object($value) && !$this->entityManager->contains($value)) {
84+
throw new \RuntimeException(sprintf(
85+
'Entity %s in field %s is not managed by EntityManager',
86+
get_class($value),
87+
$field
88+
));
89+
}
7790
}
7891
}
7992

80-
return $entity;
93+
return $this->denormalizer->denormalize(
94+
$processedData,
95+
$resourceClass,
96+
null,
97+
[ExportAwareItemNormalizer::EXPORT_CONTEXT_KEY => true]
98+
);
8199
}
82100

83101
private function processNestedArray(array $data): array

src/Denormalizer/DoctrineRelationResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function resolveEntity(string $entityClass, array $data): ?object
5252
$identifierField,
5353
$identifier
5454
));
55+
return null;
56+
}
57+
58+
// Ensure entity is managed by EntityManager to prevent cascade persist errors
59+
if (!$this->entityManager->contains($entity)) {
60+
$entity = $this->entityManager->merge($entity);
5561
}
5662

5763
return $entity;

0 commit comments

Comments
 (0)