Doctrine\ODM\MongoDB\DocumentRepository::find PHP Method

find() public method

Finds a document matching the specified identifier. Optionally a lock mode and expected version may be specified.
public find ( mixed $id, integer $lockMode = LockMode::NONE, integer $lockVersion = null ) : object | null
$id mixed Identifier.
$lockMode integer Optional. Lock mode; one of the LockMode constants.
$lockVersion integer Optional. Expected version.
return object | null The document, if found, otherwise null.
    public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
    {
        if ($id === null) {
            return null;
        }
        /* TODO: What if the ID object has a field with the same name as the
         * class' mapped identifier field name?
         */
        if (is_array($id)) {
            list($identifierFieldName) = $this->class->getIdentifierFieldNames();
            if (isset($id[$identifierFieldName])) {
                $id = $id[$identifierFieldName];
            }
        }
        // Check identity map first
        if ($document = $this->uow->tryGetById($id, $this->class)) {
            if ($lockMode !== LockMode::NONE) {
                $this->dm->lock($document, $lockMode, $lockVersion);
            }
            return $document;
            // Hit!
        }
        $criteria = array('_id' => $id);
        if ($lockMode === LockMode::NONE) {
            return $this->getDocumentPersister()->load($criteria);
        }
        if ($lockMode === LockMode::OPTIMISTIC) {
            if (!$this->class->isVersioned) {
                throw LockException::notVersioned($this->documentName);
            }
            if ($document = $this->getDocumentPersister()->load($criteria)) {
                $this->uow->lock($document, $lockMode, $lockVersion);
            }
            return $document;
        }
        return $this->getDocumentPersister()->load($criteria, null, array(), $lockMode);
    }

Usage Example

 /**
  * @param Projection $projection
  */
 public function remove(Projection $projection)
 {
     $storedProjection = $this->repository->find($projection->getProjectionName() . '_' . $projection->getAggregateId());
     if (!empty($storedProjection)) {
         $this->manager->remove($storedProjection);
     }
 }
All Usage Examples Of Doctrine\ODM\MongoDB\DocumentRepository::find