Doctrine\ODM\CouchDB\DocumentManager::getReference PHP Method

getReference() public method

Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded.
public getReference ( string $documentName, mixed $identifier ) : object
$documentName string The name of the entity type.
$identifier mixed The entity identifier.
return object The entity reference.
    public function getReference($documentName, $identifier)
    {
        $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
        // Check identity map first, if its already in there just return it.
        if ($document = $this->unitOfWork->tryGetById($identifier)) {
            return $document;
        }
        $document = $this->proxyFactory->getProxy($class->name, $identifier);
        $this->unitOfWork->registerManaged($document, $identifier, null);
        return $document;
    }

Usage Example

コード例 #1
0
 public function resolveJsonField(ClassMetadata $class, DocumentManager $dm, $documentState, $jsonName, $originalData)
 {
     $uow = $dm->getUnitOfWork();
     $couchClient = $dm->getCouchDBClient();
     if ($jsonName == 'doctrine_metadata' && isset($originalData['doctrine_metadata']['associations'])) {
         foreach ($originalData['doctrine_metadata']['associations'] as $assocName) {
             $assocValue = $originalData[$assocName];
             if (isset($class->associationsMappings[$assocName])) {
                 if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::TO_ONE) {
                     if ($assocValue) {
                         if ($class->associationsMappings[$assocName]['targetDocument']) {
                             $assocValue = $dm->getReference($class->associationsMappings[$assocName]['targetDocument'], $assocValue);
                         } else {
                             $response = $couchClient->findDocument($assocValue);
                             if ($response->status == 404) {
                                 $assocValue = null;
                             } else {
                                 $hints = array();
                                 $assocValue = $uow->createDocument(null, $response->body, $hints);
                             }
                         }
                     }
                     $documentState[$class->associationsMappings[$assocName]['fieldName']] = $assocValue;
                 } else {
                     if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::MANY_TO_MANY) {
                         if ($class->associationsMappings[$assocName]['isOwning']) {
                             $documentState[$class->associationsMappings[$assocName]['fieldName']] = new PersistentIdsCollection(new ArrayCollection(), $class->associationsMappings[$assocName]['targetDocument'], $dm, $assocValue);
                         }
                     }
                 }
             }
         }
     }
     return $documentState;
 }