Created
April 27, 2021 10:22
-
-
Save andreymeretsky/4704af52c7270c1f58e1e3ed169f57cb to your computer and use it in GitHub Desktop.
Merge Doctrine
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
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