Skip to content

Instantly share code, notes, and snippets.

@andreymeretsky
Created April 27, 2021 10:22
Show Gist options
  • Save andreymeretsky/4704af52c7270c1f58e1e3ed169f57cb to your computer and use it in GitHub Desktop.
Save andreymeretsky/4704af52c7270c1f58e1e3ed169f57cb to your computer and use it in GitHub Desktop.
Merge Doctrine
function myMerge(\Doctrine\ORM\EntityManager $em, $newObject)
{
$newObjectId = $em->getClassMetadata(get_class($newObject))->getSingleIdReflectionProperty()->getValue($newObject);
$existingObject = $em->getRepository(get_class($newObject))->find($newObjectId);
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach (get_object_vars($existingObject) 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