Doctrine\ORM\EntityManager::getPartialReference PHP Méthode

getPartialReference() public méthode

The returned reference may be a partial object if the entity is not yet loaded/managed. If it is a partial object it will not initialize the rest of the entity state on access. Thus you can only ever safely access the identifier of an entity obtained through this method. The use-cases for partial references involve maintaining bidirectional associations without loading one side of the association or to update an entity without loading it. Note, however, that in the latter case the original (persistent) entity data will never be visible to the application (especially not event listeners) as it will never be loaded in the first place.
public getPartialReference ( string $entityName, mixed $identifier ) : object
$entityName string The name of the entity type.
$identifier mixed The entity identifier.
Résultat object The (partial) entity reference.
    public function getPartialReference($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;
        }
        if ( ! is_array($identifier)) {
            $identifier = array($class->identifier[0] => $identifier);
        }

        $entity = $class->newInstance();
        $class->setIdentifierValues($entity, $identifier);
        $this->unitOfWork->registerManaged($entity, $identifier, array());

        return $entity;
    }

Usage Example

 function it_should_delete_entity(ManagerRegistry $registry, EntityManager $em)
 {
     $em->getPartialReference('foo', 1)->willReturn(42);
     $em->remove(42)->shouldBeCalled();
     $em->flush()->shouldBeCalled();
     $registry->getManager(null)->willReturn($em);
     $this->setRegistry($registry);
     $this->delete(1)->shouldBe(true);
 }
All Usage Examples Of Doctrine\ORM\EntityManager::getPartialReference