private function cascadeMerge($document, $managedCopy, array &$visited)
{
$class = $this->dm->getClassMetadata(get_class($document));
$associationMappings = array_filter($class->associationMappings, function ($assoc) {
return $assoc['isCascadeMerge'];
});
foreach ($associationMappings as $assoc) {
$relatedDocuments = $class->reflFields[$assoc['fieldName']]->getValue($document);
if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) {
if ($relatedDocuments === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) {
// Collections are the same, so there is nothing to do
continue;
}
foreach ($relatedDocuments as $relatedDocument) {
$this->doMerge($relatedDocument, $visited, $managedCopy, $assoc);
}
} elseif ($relatedDocuments !== null) {
$this->doMerge($relatedDocuments, $visited, $managedCopy, $assoc);
}
}
}