find() 공개 메소드

This is just a convenient shortcut for getRepository($documentName)->find($id).
public find ( string $documentName, mixed $identifier, integer $lockMode = LockMode::NONE, integer $lockVersion = null ) : object
$documentName string
$identifier mixed
$lockMode integer
$lockVersion integer
리턴 object $document
 /**
  * @param  string $id
  * @return Recipe
  * @throws NotFoundException If no Recipe found
  */
 public function find($id)
 {
     $recipe = $this->dm->find('Hellofresh\\DoctrineTutorial\\Menu\\Recipe', $id);
     if (!$recipe) {
         throw new NotFoundException("Recipe not found");
     }
     return $recipe;
 }
예제 #2
0
 /**
  * Get get cart products for given $userId
  *
  * @param int $productId
  *
  * @return Product $product
  */
 public function loadProductById($productId)
 {
     $product = $this->dm->find('Aisel\\ProductBundle\\Document\\Product', $productId);
     if (!$product) {
         throw new LogicException('Product was not found');
     }
     return $product;
 }
예제 #3
0
 /**
  * @depends testLang
  * @param $lang
  */
 public function testTranslationDelete($lang)
 {
     $language = $this->documentManager->find(get_class($lang->language), $lang->language->getId());
     $translation = $this->documentManager->find(get_class($lang->translation), $lang->translation->getId());
     $language->removeTranslation($translation);
     $this->documentManager->flush();
     $this->documentManager->remove($translation);
     $this->documentManager->remove($language);
     $this->documentManager->flush();
 }
 public function testReferenceOnePreload()
 {
     $question = new BasicQuestion();
     $answer = $this->persistQuestionWithAnswer($question);
     $this->dm->find(get_class($answer), $answer->id);
     $receivedQuestion = $this->dm->find(get_class($question), $question->id);
     $this->assertTrue($receivedQuestion->answer instanceof CorrectAnswer);
 }
예제 #5
0
 /**
  * Returns the document for the given key
  *
  * They are either fetched from the
  * internal document cache (if filled) or loaded from the database.
  *
  * @param  string $key  The choice key document ID
  * @return object       The matching document
  */
 public function getDocument($key)
 {
     if (!$this->loaded) {
         $this->load();
     }
     if ($this->documents) {
         return isset($this->documents[$key]) ? $this->documents[$key] : null;
     }
     return $this->documentManager->find($this->class, $key);
 }
예제 #6
0
 /**
  * Executes a merge operation on a document.
  *
  * @param object      $document
  * @param array       $visited
  * @param object|null $prevManagedCopy
  * @param array|null  $assoc
  *
  * @return object The managed copy of the document.
  *
  * @throws InvalidArgumentException If the entity instance is NEW.
  * @throws LockException If the document uses optimistic locking through a
  *                       version attribute and the version check against the
  *                       managed copy fails.
  */
 private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null)
 {
     $oid = spl_object_hash($document);
     if (isset($visited[$oid])) {
         return $visited[$oid];
         // Prevent infinite recursion
     }
     $visited[$oid] = $document;
     // mark visited
     $class = $this->dm->getClassMetadata(get_class($document));
     /* First we assume DETACHED, although it can still be NEW but we can
      * avoid an extra DB round trip this way. If it is not MANAGED but has
      * an identity, we need to fetch it from the DB anyway in order to
      * merge. MANAGED documents are ignored by the merge operation.
      */
     $managedCopy = $document;
     if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) {
         if ($document instanceof Proxy && !$document->__isInitialized()) {
             $document->__load();
         }
         // Try to look the document up in the identity map.
         $id = $class->isEmbeddedDocument ? null : $class->getIdentifierObject($document);
         if ($id === null) {
             // If there is no identifier, it is actually NEW.
             $managedCopy = $class->newInstance();
             $this->persistNew($class, $managedCopy);
         } else {
             $managedCopy = $this->tryGetById($id, $class);
             if ($managedCopy) {
                 // We have the document in memory already, just make sure it is not removed.
                 if ($this->getDocumentState($managedCopy) === self::STATE_REMOVED) {
                     throw new \InvalidArgumentException('Removed entity detected during merge. Cannot merge with a removed entity.');
                 }
             } else {
                 // We need to fetch the managed copy in order to merge.
                 $managedCopy = $this->dm->find($class->name, $id);
             }
             if ($managedCopy === null) {
                 // If the identifier is ASSIGNED, it is NEW
                 $managedCopy = $class->newInstance();
                 $class->setIdentifierValue($managedCopy, $id);
                 $this->persistNew($class, $managedCopy);
             } else {
                 if ($managedCopy instanceof Proxy && !$managedCopy->__isInitialized__) {
                     $managedCopy->__load();
                 }
             }
         }
         if ($class->isVersioned) {
             $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy);
             $documentVersion = $class->reflFields[$class->versionField]->getValue($document);
             // Throw exception if versions don't match
             if ($managedCopyVersion != $documentVersion) {
                 throw LockException::lockFailedVersionMissmatch($document, $documentVersion, $managedCopyVersion);
             }
         }
         // Merge state of $document into existing (managed) document
         foreach ($class->reflClass->getProperties() as $prop) {
             $name = $prop->name;
             $prop->setAccessible(true);
             if (!isset($class->associationMappings[$name])) {
                 if (!$class->isIdentifier($name)) {
                     $prop->setValue($managedCopy, $prop->getValue($document));
                 }
             } else {
                 $assoc2 = $class->associationMappings[$name];
                 if ($assoc2['type'] === 'one') {
                     $other = $prop->getValue($document);
                     if ($other === null) {
                         $prop->setValue($managedCopy, null);
                     } elseif ($other instanceof Proxy && !$other->__isInitialized__) {
                         // Do not merge fields marked lazy that have not been fetched
                         continue;
                     } elseif (!$assoc2['isCascadeMerge']) {
                         if ($this->getDocumentState($other) === self::STATE_DETACHED) {
                             $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other);
                             /* @var $targetClass \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */
                             $targetClass = $this->dm->getClassMetadata($targetDocument);
                             $relatedId = $targetClass->getIdentifierObject($other);
                             if ($targetClass->subClasses) {
                                 $other = $this->dm->find($targetClass->name, $relatedId);
                             } else {
                                 $other = $this->dm->getProxyFactory()->getProxy($assoc2['targetDocument'], array($targetClass->identifier => $relatedId));
                                 $this->registerManaged($other, $relatedId, array());
                             }
                         }
                         $prop->setValue($managedCopy, $other);
                     }
                 } else {
                     $mergeCol = $prop->getValue($document);
                     if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) {
                         /* Do not merge fields marked lazy that have not
                          * been fetched. Keep the lazy persistent collection
                          * of the managed copy.
                          */
                         continue;
                     }
                     $managedCol = $prop->getValue($managedCopy);
                     if (!$managedCol) {
                         $managedCol = new PersistentCollection(new ArrayCollection(), $this->dm, $this);
                         $managedCol->setOwner($managedCopy, $assoc2);
                         $prop->setValue($managedCopy, $managedCol);
                         $this->originalDocumentData[$oid][$name] = $managedCol;
                     }
                     /* Note: do not process association's target documents.
                      * They will be handled during the cascade. Initialize
                      * and, if necessary, clear $managedCol for now.
                      */
                     if ($assoc2['isCascadeMerge']) {
                         $managedCol->initialize();
                         // If $managedCol differs from the merged collection, clear and set dirty
                         if (!$managedCol->isEmpty() && $managedCol !== $mergeCol) {
                             $managedCol->unwrap()->clear();
                             $managedCol->setDirty(true);
                             if ($assoc2['isOwningSide'] && $class->isChangeTrackingNotify()) {
                                 $this->scheduleForDirtyCheck($managedCopy);
                             }
                         }
                     }
                 }
             }
             if ($class->isChangeTrackingNotify()) {
                 // Just treat all properties as changed, there is no other choice.
                 $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
             }
         }
         if ($class->isChangeTrackingDeferredExplicit()) {
             $this->scheduleForDirtyCheck($document);
         }
     }
     if ($prevManagedCopy !== null) {
         $assocField = $assoc['fieldName'];
         $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy));
         if ($assoc['type'] === 'one') {
             $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
         } else {
             $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy);
             if ($assoc['type'] === 'many' && isset($assoc['mappedBy'])) {
                 $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy);
             }
         }
     }
     // Mark the managed copy visited as well
     $visited[spl_object_hash($managedCopy)] = true;
     $this->cascadeMerge($document, $managedCopy, $visited);
     return $managedCopy;
 }
 /**
  * Executes a merge operation on an document.
  *
  * @param object $document
  * @param array $visited
  * @return object The managed copy of the document.
  * @throws InvalidArgumentException If the document instance is NEW.
  */
 private function _doMerge($document, array &$visited, $prevManagedCopy = null, $mapping = null)
 {
     $class = $this->_dm->getClassMetadata(get_class($document));
     $id = $class->getIdentifierValues($document);
     if (!$id) {
         throw new \InvalidArgumentException('New document detected during merge.' . ' Persist the new document before merging.');
     }
     // MANAGED documents are ignored by the merge operation
     if ($this->getDocumentState($document, self::STATE_DETACHED) == self::STATE_MANAGED) {
         $managedCopy = $document;
     } else {
         // Try to look the document up in the identity map.
         $managedCopy = $this->tryGetById($id, $class->rootDocumentName);
         if ($managedCopy) {
             // We have the document in-memory already, just make sure its not removed.
             if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) {
                 throw new \InvalidArgumentException('Removed document detected during merge.' . ' Can not merge with a removed document.');
             }
         } else {
             // We need to fetch the managed copy in order to merge.
             $managedCopy = $this->_dm->find($class->name, $id);
         }
         if ($managedCopy === null) {
             throw new \InvalidArgumentException('New document detected during merge.' . ' Persist the new document before merging.');
         }
         // Merge state of $document into existing (managed) document
         foreach ($class->reflFields as $name => $prop) {
             if (!isset($class->fieldMappings[$name]['reference'])) {
                 $prop->setValue($managedCopy, $prop->getValue($document));
             } else {
                 $mapping2 = $class->fieldMappings[$name];
                 if ($mapping2['type'] === 'one') {
                     if (!$assoc2['isCascadeMerge']) {
                         $other = $class->reflFields[$name]->getValue($document);
                         //TODO: Just $prop->getValue($document)?
                         if ($other !== null) {
                             $targetClass = $this->_dm->getClassMetadata($mapping2['targetDocument']);
                             $id = $targetClass->getIdentifierValue($other);
                             $proxy = $this->_dm->getProxyFactory()->getProxy($mapping2['targetDocument'], $id);
                             $prop->setValue($managedCopy, $proxy);
                             $this->registerManaged($proxy, $id, array());
                         }
                     }
                 } else {
                     $coll = new PersistentCollection($this->_dm, $this->_dm->getClassMetadata($mapping2['targetDocument']), new ArrayCollection());
                     $coll->setOwner($managedCopy, $mapping2);
                     $coll->setInitialized($mapping2['isCascadeMerge']);
                     $prop->setValue($managedCopy, $coll);
                 }
             }
         }
     }
     if ($prevManagedCopy !== null) {
         $assocField = $mapping['fieldName'];
         $prevClass = $this->_dm->getClassMetadata(get_class($prevManagedCopy));
         if ($mapping['type'] === 'one') {
             $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
         } else {
             $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap()->add($managedCopy);
         }
     }
     $this->_cascadeMerge($document, $managedCopy, $visited);
     return $managedCopy;
 }
예제 #8
0
 /**
  * Executes a merge operation on an document.
  *
  * @param object $document
  * @param array $visited
  * @return object The managed copy of the document.
  * @throws InvalidArgumentException If the document instance is NEW.
  */
 private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null)
 {
     $oid = spl_object_hash($document);
     if (isset($visited[$oid])) {
         return;
         // Prevent infinite recursion
     }
     $visited[$oid] = $document;
     // mark visited
     $class = $this->dm->getClassMetadata(get_class($document));
     // First we assume DETACHED, although it can still be NEW but we can avoid
     // an extra db-roundtrip this way. If it is not MANAGED but has an identity,
     // we need to fetch it from the db anyway in order to merge.
     // MANAGED documents are ignored by the merge operation.
     if ($this->getDocumentState($document, self::STATE_DETACHED) == self::STATE_MANAGED) {
         $managedCopy = $document;
     } else {
         // Try to look the entity up in the identity map.
         $id = $class->getIdentifierValue($document);
         // If there is no ID, it is actually NEW.
         if (!$id) {
             $managedCopy = $class->newInstance();
             $this->persistNew($class, $managedCopy);
         } else {
             $managedCopy = $this->tryGetById($id, $class->rootDocumentName);
             if ($managedCopy) {
                 // We have the entity in-memory already, just make sure its not removed.
                 if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) {
                     throw new InvalidArgumentException('Removed entity detected during merge.' . ' Can not merge with a removed entity.');
                 }
             } else {
                 // We need to fetch the managed copy in order to merge.
                 $managedCopy = $this->dm->find($class->name, $id);
             }
             if ($managedCopy === null) {
                 // If the identifier is ASSIGNED, it is NEW, otherwise an error
                 // since the managed entity was not found.
                 $managedCopy = $class->newInstance();
                 $class->setIdentifierValues($managedCopy, $id);
                 $this->persistNew($class, $managedCopy);
             }
         }
         // Merge state of $document into existing (managed) entity
         foreach ($class->reflFields as $name => $prop) {
             if (!isset($class->fieldMappings[$name]['embedded']) && !isset($class->fieldMappings[$name]['reference'])) {
                 $prop->setValue($managedCopy, $prop->getValue($document));
             } else {
                 $assoc2 = $class->fieldMappings[$name];
                 if ($assoc2['type'] === 'one') {
                     $other = $prop->getValue($document);
                     if ($other === null) {
                         $prop->setValue($managedCopy, null);
                     } else {
                         if ($other instanceof Proxy && !$other->__isInitialized__) {
                             // do not merge fields marked lazy that have not been fetched.
                             continue;
                         } else {
                             if (!$assoc2['isCascadeMerge']) {
                                 if ($this->getDocumentState($other, self::STATE_DETACHED) == self::STATE_MANAGED) {
                                     $prop->setValue($managedCopy, $other);
                                 } else {
                                     $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other);
                                     $targetClass = $this->dm->getClassMetadata($targetDocument);
                                     $id = $targetClass->getIdentifierValue($other);
                                     $proxy = $this->dm->getProxyFactory()->getProxy($targetDocument, $id);
                                     $prop->setValue($managedCopy, $proxy);
                                     $this->registerManaged($proxy, $id, array());
                                 }
                             }
                         }
                     }
                 } else {
                     $mergeCol = $prop->getValue($document);
                     if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) {
                         // do not merge fields marked lazy that have not been fetched.
                         // keep the lazy persistent collection of the managed copy.
                         continue;
                     }
                     foreach ($mergeCol as $entry) {
                         $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($entry);
                         $targetClass = $this->dm->getClassMetadata($targetDocument);
                         if ($targetClass->isEmbeddedDocument) {
                             $this->registerManagedEmbeddedDocument($entry, array());
                         } else {
                             $id = $targetClass->getIdentifierValue($entry);
                             $this->registerManaged($entry, $id, array());
                         }
                     }
                     if (!$mergeCol instanceof PersistentCollection) {
                         $mergeCol = new PersistentCollection($mergeCol, $this->dm);
                         $mergeCol->setOwner($managedCopy, $assoc2);
                         $mergeCol->setInitialized(true);
                     }
                     $prop->setValue($managedCopy, $mergeCol);
                 }
             }
             if ($class->isChangeTrackingNotify()) {
                 // Just treat all properties as changed, there is no other choice.
                 $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
             }
         }
         if ($class->isChangeTrackingDeferredExplicit()) {
             $this->scheduleForDirtyCheck($document);
         }
     }
     if ($prevManagedCopy !== null) {
         $assocField = $assoc->sourceFieldName;
         $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy));
         if ($assoc->isOneToOne()) {
             $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
         } else {
             $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap()->add($managedCopy);
             if ($assoc->isOneToMany()) {
                 $class->reflFields[$assoc->mappedBy]->setValue($managedCopy, $prevManagedCopy);
             }
         }
     }
     // Mark the managed copy visited as well
     $visited[spl_object_hash($managedCopy)] = true;
     $this->cascadeMerge($document, $managedCopy, $visited);
     return $managedCopy;
 }
예제 #9
0
 public function find($id)
 {
     return $this->om->find($this->class, $id);
 }
예제 #10
0
 /**
  * Executes a refresh operation on an document.
  * 
  * @param object $document The document to refresh.
  * @param array $visited The already visited documents during cascades.
  * @throws InvalidArgumentException If the document is not MANAGED.
  */
 private function _doRefresh($document, array &$visited)
 {
     $oid = spl_object_hash($document);
     if (isset($visited[$oid])) {
         return;
         // Prevent infinite recursion
     }
     $visited[$oid] = $document;
     // mark visited
     $class = $this->_dm->getClassMetadata(get_class($document));
     if ($this->getDocumentState($document) == self::STATE_MANAGED) {
         $this->_dm->find($class->name, $this->_documentIdentifiers[$oid], true);
     } else {
         throw new \InvalidArgumentException("Document is not MANAGED.");
     }
     $this->_cascadeRefresh($document, $visited);
 }