Doctrine\ODM\PHPCR\UnitOfWork::getOrCreateProxy PHP Method

getOrCreateProxy() public method

Get the existing document or proxy for this id of this class, or create a new one.
public getOrCreateProxy ( string $targetId, string $className, string $locale = null ) : object
$targetId string
$className string
$locale string
return object
    public function getOrCreateProxy($targetId, $className, $locale = null)
    {
        $document = $this->getDocumentById($targetId);
        // check if referenced document already exists
        if ($document) {
            $metadata = $this->dm->getClassMetadata($className);
            if ($locale && $locale !== $this->getCurrentLocale($document, $metadata)) {
                $this->doLoadTranslation($document, $metadata, $locale, true);
            }
            return $document;
        }
        $metadata = $this->dm->getClassMetadata($className);
        $proxyDocument = $this->dm->getProxyFactory()->getProxy($className, array($metadata->identifier => $targetId));
        // register the document under its own id
        $this->registerDocument($proxyDocument, $targetId);
        if ($locale) {
            $this->setLocale($proxyDocument, $this->dm->getClassMetadata($className), $locale);
        }
        return $proxyDocument;
    }

Usage Example

Beispiel #1
0
 /**
  * {@inheritDoc}
  */
 public function getReference($documentName, $id)
 {
     return $this->unitOfWork->getOrCreateProxy($id, $documentName);
 }
UnitOfWork