本文整理汇总了PHP中Doctrine\ODM\MongoDB\UnitOfWork::tryGetById方法的典型用法代码示例。如果您正苦于以下问题:PHP UnitOfWork::tryGetById方法的具体用法?PHP UnitOfWork::tryGetById怎么用?PHP UnitOfWork::tryGetById使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ODM\MongoDB\UnitOfWork
的用法示例。
在下文中一共展示了UnitOfWork::tryGetById方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: find
/**
* Finds a document by its identifier
*
* @throws LockException
* @param string|object $id The identifier
* @param int $lockMode
* @param int $lockVersion
* @return object The document.
*/
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
{
if ($id === null) {
return;
}
if (is_array($id)) {
list($identifierFieldName) = $this->class->getIdentifierFieldNames();
if (!isset($id[$identifierFieldName])) {
throw MongoDBException::missingIdentifierField($this->documentName, $identifierFieldName);
}
$id = $id[$identifierFieldName];
}
// Check identity map first
if ($document = $this->uow->tryGetById($id, $this->class->rootDocumentName)) {
if ($lockMode != LockMode::NONE) {
$this->dm->lock($document, $lockMode, $lockVersion);
}
return $document;
// Hit!
}
if ($lockMode == LockMode::NONE) {
return $this->uow->getDocumentPersister($this->documentName)->load($id);
} else {
if ($lockMode == LockMode::OPTIMISTIC) {
if (!$this->class->isVersioned) {
throw LockException::notVersioned($this->documentName);
}
$document = $this->uow->getDocumentPersister($this->documentName)->load($id);
$this->uow->lock($document, $lockMode, $lockVersion);
return $document;
} else {
return $this->uow->getDocumentPersister($this->documentName)->load($id, null, array(), $lockMode);
}
}
}
示例2: find
/**
* Finds a document by its identifier.
*
* @param $id The identifier.
* @param int $lockMode
* @param int $lockVersion
* @return object The document.
*/
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
{
// Check identity map first
if ($document = $this->uow->tryGetById($id, $this->class->rootDocumentName)) {
if ($lockMode != LockMode::NONE) {
$this->dm->lock($document, $lockMode, $lockVersion);
}
return $document;
// Hit!
}
$id = array('_id' => $id);
if ($lockMode == LockMode::NONE) {
return $this->uow->getDocumentPersister($this->documentName)->load($id);
} else {
if ($lockMode == LockMode::OPTIMISTIC) {
if (!$this->class->isVersioned) {
throw LockException::notVersioned($this->documentName);
}
$document = $this->uow->getDocumentPersister($this->documentName)->load($id);
$this->uow->lock($document, $lockMode, $lockVersion);
return $document;
} else {
return $this->uow->getDocumentPersister($this->documentName)->load($id, null, array(), $lockMode);
}
}
}
示例3: primeCollection
/**
* Prime a collection of documents property with an efficient single query instead of
* lazily loading each field with a single query.
*
* @param Iterator $collection
* @param string $fieldName
* @param Closure|boolean $primer
* @param array $hints
*/
public function primeCollection(Iterator $collection, $fieldName, $primer, array $hints = array())
{
$collection = $collection->toArray();
if (!count($collection)) {
return;
}
$collectionMetaData = $this->dm->getClassMetaData(get_class(current($collection)));
$fieldMapping = $collectionMetaData->fieldMappings[$fieldName];
$cmd = $this->cmd;
$groupedIds = array();
foreach ($collection as $element) {
if ($fieldMapping['type'] == 'many') {
$fieldValue = $collectionMetaData->getFieldValue($element, $fieldName);
if ($fieldValue instanceof PersistentCollection) {
foreach ($fieldValue->getMongoData() as $key => $reference) {
if (isset($fieldMapping['simple']) && $fieldMapping['simple']) {
$className = $fieldMapping['targetDocument'];
$mongoId = $reference;
} else {
$className = $this->dm->getClassNameFromDiscriminatorValue($fieldMapping, $reference);
$mongoId = $reference[$cmd . 'id'];
}
$id = (string) $mongoId;
$document = $this->uow->tryGetById($id, $className);
if (!$document || $document instanceof Proxy && !$document->__isInitialized__) {
if (!isset($groupedIds[$className])) {
$groupedIds[$className] = array();
}
$groupedIds[$className][] = $mongoId;
}
}
}
} else {
if ($fieldMapping['type'] == 'one') {
$document = $collectionMetaData->getFieldValue($element, $fieldName);
if ($document && $document instanceof Proxy && !$document->__isInitialized__) {
$class = $this->dm->getClassMetadata(get_class($document));
$groupedIds[$class->name][] = $this->uow->getDocumentIdentifier($document);
}
}
}
}
foreach ($groupedIds as $className => $ids) {
$class = $this->dm->getClassMetadata($className);
if ($primer instanceof \Closure) {
$primer($this->dm, $className, $fieldName, $ids, $hints);
} else {
$repository = $this->dm->getRepository($className);
$qb = $repository->createQueryBuilder()->field($class->identifier)->in($ids);
if (isset($hints[Query::HINT_SLAVE_OKAY])) {
$qb->slaveOkay(true);
}
$query = $qb->getQuery();
$query->execute()->toArray();
}
}
}
示例4: getPartialReference
/**
* Gets a partial reference to the document identified by the given type and identifier
* without actually loading it, if the document is not yet loaded.
*
* The returned reference may be a partial object if the document is not yet loaded/managed.
* If it is a partial object it will not initialize the rest of the document state on access.
* Thus you can only ever safely access the identifier of an document 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 document without loading it.
* Note, however, that in the latter case the original (persistent) document data will
* never be visible to the application (especially not event listeners) as it will
* never be loaded in the first place.
*
* @param string $documentName The name of the document type.
* @param mixed $identifier The document identifier.
* @return object The (partial) document reference.
*/
public function getPartialReference($documentName, $identifier)
{
$class = $this->metadataFactory->getMetadataFor($documentName);
// Check identity map first, if its already in there just return it.
if ($document = $this->unitOfWork->tryGetById($identifier, $class->rootDocumentName)) {
return $document;
}
$document = $class->newInstance();
$class->setIdentifierValue($document, $identifier);
$this->unitOfWork->registerManaged($document, $identifier, array());
return $document;
}
示例5: addManyReferences
/**
* Adds identifiers from a PersistentCollection to $groupedIds.
*
* If the relation contains simple references, the mapping is assumed to
* have a target document class defined. Without that, there is no way to
* infer the class of the referenced documents.
*
* @param PersistentCollection $persistentCollection
* @param array $groupedIds
*/
private function addManyReferences(PersistentCollection $persistentCollection, array &$groupedIds)
{
$mapping = $persistentCollection->getMapping();
if (!empty($mapping['simple'])) {
$className = $mapping['targetDocument'];
$class = $this->dm->getClassMetadata($className);
}
foreach ($persistentCollection->getMongoData() as $reference) {
if (!empty($mapping['simple'])) {
$id = $reference;
} else {
$id = $reference['$id'];
$className = $this->uow->getClassNameForAssociation($mapping, $reference);
$class = $this->dm->getClassMetadata($className);
}
$document = $this->uow->tryGetById($id, $class);
if (!$document || $document instanceof Proxy && !$document->__isInitialized()) {
$id = $class->getPHPIdentifierValue($id);
$groupedIds[$className][serialize($id)] = $id;
}
}
}
示例6: find
/**
* Finds a document by its identifier
*
* @param string|object $id The identifier
* @param int $lockMode
* @param int $lockVersion
* @throws Mapping\MappingException
* @throws LockException
* @return object The document.
*/
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
{
if ($id === null) {
return;
}
/* 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);
}