Skip to content

Commit

Permalink
Merge pull request #1580 from ConductionNL/feature/DIT-197/hydrate-fix
Browse files Browse the repository at this point in the history
Make sure to not create new (duplicate) subobjects when we don't need to
  • Loading branch information
WilcoLouwerse authored Oct 30, 2023
2 parents 746340a + 8b01cae commit 40cf6f5
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions api/src/Entity/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,21 +659,38 @@ public function setValue($value, bool $unsafe = false, ?DateTimeInterface $dateM
foreach ($valueArray as $value) {
// Catch Array input (for hydrator)
if (is_array($value)) {
$object = new ObjectEntity($this->getAttribute()->getObject());
$object = null;

// Make sure to not create new objects if we don't have to...
if (isset($value['id'])) {
$objects = $this->objects->filter(function ($item) use ($value) {
return $item->getId() !== null && $item->getId()->toString() === $value['id'];
});

if (count($objects) > 0) {
$object = $objects[0];
}
}
if ($object instanceof ObjectEntity === false) {
// failsafe to not create duplicate sub objects. In some weird cases $objects[0] doesn't return an ObjectEntity.
if (isset($objects) === true && count($objects) > 0) {
continue;
}
$object = new ObjectEntity($this->getAttribute()->getObject());
}

$object->setOwner($this->getObjectEntity()->getOwner());
$object->setApplication($this->getObjectEntity()->getApplication());
$object->setOrganization($this->getObjectEntity()->getOrganization());
$object->hydrate($value, $unsafe, $dateModified);
$value = $object;
$this->hydratedObjects[] = $object;
}

if (is_string($value)) {
$idArray[] = $value;
} elseif (!$value) {
continue;
} elseif ($value instanceof ObjectEntity) {
} elseif ($value instanceof ObjectEntity && $this->objects->contains($value) === false) {
$this->addObject($value);
}
}
Expand Down

0 comments on commit 40cf6f5

Please sign in to comment.