/** * Generates a closure capable of finalizing a cloned proxy * * @param ClassMetadata $class * @param DocumentPersister $documentPersister * @param \ReflectionProperty $reflectionId * * @return callable */ private function createCloner(ClassMetadata $class, DocumentPersister $documentPersister, \ReflectionProperty $reflectionId) { return function (BaseProxy $proxy) use($reflectionId, $documentPersister, $class) { if ($proxy->__isInitialized()) { return; } $proxy->__setInitialized(true); $proxy->__setInitializer(null); $rid = $reflectionId->getValue($proxy); $original = $documentPersister->load($rid); if (null === $original) { throw DocumentNotFoundException::documentNotFound(get_class($proxy), $rid); } foreach ($class->reflFields as $name => $property) { $property->setValue($proxy, $property->getValue($original)); } }; }
/** * Generates a closure capable of finalizing a cloned proxy * * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata * @param \ReflectionProperty $reflectionId * * @return \Closure * * @throws \Doctrine\ODM\OrientDB\DocumentNotFoundException */ private function createCloner(BaseClassMetadata $classMetadata, Hydrator $hydrator, \ReflectionProperty $reflectionId) { return function (BaseProxy $proxy) use($reflectionId, $hydrator, $classMetadata) { if ($proxy->__isInitialized()) { return; } $proxy->__setInitialized(true); $proxy->__setInitializer(null); $rid = $reflectionId->getValue($proxy); $original = $hydrator->load(array($rid)); if (null === $original) { throw DocumentNotFoundException::documentNotFound(get_class($proxy), $rid); } $original = $hydrator->hydrate($original[0], $proxy); 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)); } } }; }