Created
December 23, 2020 07:47
-
-
Save stevro/99060106bbe54d64d3fbcf9a61e6a273 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
//$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