/** * Gets a reference to the entity identified by the given type and identifier * without actually loading it, if the entity is not yet loaded. * * @param string $entityName The name of the entity type. * @param mixed $id The entity identifier. * @return object The entity reference. */ public function getReference($entityName, $id) { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); if (!is_array($id)) { $id = array($class->identifier[0] => $id); } $sortedId = array(); foreach ($class->identifier as $identifier) { if (!isset($id[$identifier])) { throw ORMException::missingIdentifierField($class->name, $identifier); } $sortedId[$identifier] = $id[$identifier]; } // Check identity map first, if its already in there just return it. if ($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) { return $entity instanceof $class->name ? $entity : null; } if ($class->subClasses) { return $this->find($entityName, $sortedId); } if (!is_array($sortedId)) { $sortedId = array($class->identifier[0] => $sortedId); } $entity = $this->proxyFactory->getProxy($class->name, $sortedId); $this->unitOfWork->registerManaged($entity, $sortedId, array()); return $entity; }
/** * @group DDC-1771 */ public function testSkipAbstractClassesOnGeneration() { $cm = new \Doctrine\ORM\Mapping\ClassMetadata(__NAMESPACE__ . '\\AbstractClass'); $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService()); $this->assertNotNull($cm->reflClass); $num = $this->_proxyFactory->generateProxyClasses(array($cm)); $this->assertEquals(0, $num, "No proxies generated."); }
public function testClassWithSleepProxyGeneration() { $className = "\\Doctrine\\Tests\\ORM\\Proxy\\SleepClass"; $proxyName = "DoctrineTestsORMProxySleepClassProxy"; $classMetadata = new \Doctrine\ORM\Mapping\ClassMetadata($className); $classMetadata->mapField(array('fieldName' => 'id', 'type' => 'integer')); $classMetadata->setIdentifier(array('id')); $this->_proxyFactory->generateProxyClasses(array($classMetadata)); $classCode = file_get_contents(dirname(__FILE__) . "/generated/" . $proxyName . ".php"); $this->assertEquals(1, substr_count($classCode, 'function __sleep')); }
/** * Gets a reference to the entity identified by the given type and identifier * without actually loading it. * * If partial objects are allowed, this method will return a partial object that only * has its identifier populated. Otherwise a proxy is returned that automatically * loads itself on first access. * * @return object The entity reference. */ public function getReference($entityName, $identifier) { $class = $this->_metadataFactory->getMetadataFor($entityName); // Check identity map first, if its already in there just return it. if ($entity = $this->_unitOfWork->tryGetById($identifier, $class->rootEntityName)) { return $entity; } if (!is_array($identifier)) { $identifier = array($class->identifier[0] => $identifier); } $entity = $this->_proxyFactory->getProxy($class->name, $identifier); $this->_unitOfWork->registerManaged($entity, $identifier, array()); return $entity; }
/** * @group DDC-2432 */ public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized() { $persister = $this->getMock('Doctrine\\ORM\\Persisters\\BasicEntityPersister', array('load'), array(), '', false); $this->uowMock->setEntityPersister('Doctrine\\Tests\\Models\\ECommerce\\ECommerceFeature', $persister); /* @var $proxy \Doctrine\Common\Proxy\Proxy */ $proxy = $this->proxyFactory->getProxy('Doctrine\\Tests\\Models\\ECommerce\\ECommerceFeature', array('id' => 42)); $persister->expects($this->atLeastOnce())->method('load')->will($this->returnValue(null)); try { $cloned = clone $proxy; $this->fail('An exception was expected to be raised'); } catch (EntityNotFoundException $exception) { } $this->assertFalse($proxy->__isInitialized()); $this->assertInstanceOf('Closure', $proxy->__getInitializer(), 'The initializer wasn\'t removed'); $this->assertInstanceOf('Closure', $proxy->__getCloner(), 'The cloner wasn\'t removed'); }
/** * Gets a reference to the entity identified by the given type and identifier * without actually loading it, if the entity is not yet loaded. * * @param string $entityName The name of the entity type. * @param mixed $identifier The entity identifier. * @return object The entity reference. */ public function getReference($entityName, $identifier) { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); // Check identity map first, if its already in there just return it. if ($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) { return $entity instanceof $class->name ? $entity : null; } if ($class->subClasses) { $entity = $this->find($entityName, $identifier); } else { if (!is_array($identifier)) { $identifier = array($class->identifier[0] => $identifier); } $entity = $this->proxyFactory->getProxy($class->name, $identifier); $this->unitOfWork->registerManaged($entity, $identifier, array()); } return $entity; }
/** * @param string $className * @return ProxyDefinition */ protected function createProxyDefinition($className) { $proxyDefinition = parent::createProxyDefinition($className); $doctrineInitializer = $proxyDefinition->initializer; // Doctrine\ORM\Proxy\ProxyFactory::$uow is private, so use Reflection to get it $reflectionProperty = new \ReflectionProperty(get_parent_class($this), 'uow'); $reflectionProperty->setAccessible(true); $entityPersister = $reflectionProperty->getValue($this)->getEntityPersister($className); $reflectionProperty->setAccessible(false); $initializer = function (Proxy $proxy) use($doctrineInitializer, $entityPersister) { // Doctrine\ORM\Proxy\ProxyFactory::$em is private, so use Reflection to get it $property = new \ReflectionProperty(get_class($entityPersister), 'em'); $property->setAccessible(true); /** @var EntityManagerInterface $entityManager */ $entityManager = $property->getValue($entityPersister); $property->setAccessible(false); call_user_func($doctrineInitializer, $proxy); $postLazyLoadEventArgs = new PostLazyLoadEventArgs($entityManager, $proxy); $entityManager->getEventManager()->dispatchEvent(PostLazyLoadEventArgs::EVENT_NAME, $postLazyLoadEventArgs); }; $proxyDefinition->initializer = $initializer; return $proxyDefinition; }