Skip to content

Instantly share code, notes, and snippets.

@stevro
Created December 23, 2020 07:47
Show Gist options
  • Save stevro/99060106bbe54d64d3fbcf9a61e6a273 to your computer and use it in GitHub Desktop.
Save stevro/99060106bbe54d64d3fbcf9a61e6a273 to your computer and use it in GitHub Desktop.
...
//$entityName is the class name of your Entity
//$newObject is the data you got from JSON, should be same class as $entityName
function mapData(string $entityName, $newObject){
// In findMyObject() create the query and enable result cache
$existingObject = $this->em->getRepository($entityName)->findMyObject($newObject->getId());
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach (get_object_vars($newObject) as $key => $value) {
if ($propertyAccessor->isWritable($existingObject, $key)) {
$propertyAccessor->setValue($existingObject, $key, $value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment