/** * Generates a closure capable of finalizing a cloned proxy * * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata * @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister * @param \ReflectionProperty $reflectionId * * @return \Closure * * @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException */ private function createCloner(BaseClassMetadata $classMetadata, DocumentPersister $documentPersister, ReflectionProperty $reflectionId) { return function (BaseProxy $proxy) use($documentPersister, $classMetadata, $reflectionId) { if ($proxy->__isInitialized()) { return; } $proxy->__setInitialized(true); $proxy->__setInitializer(null); $id = $reflectionId->getValue($proxy); $original = $documentPersister->load(array('_id' => $id)); if (null === $original) { if (!$this->lifecycleEventManager->documentNotFound($proxy, $id)) { throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); } } foreach ($classMetadata->getReflectionClass()->getProperties() as $reflectionProperty) { $propertyName = $reflectionProperty->getName(); if ($classMetadata->hasField($propertyName) || $classMetadata->hasAssociation($propertyName)) { $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original)); } } }; }