示例#1
0
 /**
  * Finds a document by a set of criteria.
  *
  * If a scalar or MongoId is provided for $criteria, it will be used to
  * match an _id value.
  *
  * @param mixed   $criteria Query criteria
  * @param object  $document Document to load the data into. If not specified, a new document is created.
  * @param array   $hints    Hints for document creation
  * @param integer $lockMode
  * @param array   $sort     Sort array for Cursor::sort()
  * @throws \CosmoW\ODM\Riak\LockException
  * @return object|null The loaded and managed document instance or null if no document was found
  * @todo Check identity map? loadById method? Try to guess whether $criteria is the id?
  */
 public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null)
 {
     // TODO: remove this
     if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) {
         $criteria = array('_id' => $criteria);
     }
     $criteria = $this->prepareQueryOrNewObj($criteria);
     $criteria = $this->addDiscriminatorToPreparedQuery($criteria);
     $criteria = $this->addFilterToPreparedQuery($criteria);
     $cursor = $this->collection->find($criteria);
     if (null !== $sort) {
         $cursor->sort($this->prepareSortOrProjection($sort));
     }
     $result = $cursor->getSingleResult();
     if ($this->class->isLockable) {
         $lockMapping = $this->class->fieldMappings[$this->class->lockField];
         if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) {
             throw LockException::lockFailed($result);
         }
     }
     return $this->createDocument($result, $document, $hints);
 }
示例#2
0
 /**
  * Executes a query updating the given document.
  *
  * @param object $document
  * @param array $newObj
  * @param array $options
  */
 private function executeQuery($document, array $newObj, array $options)
 {
     $className = get_class($document);
     $class = $this->dm->getClassMetadata($className);
     $id = $class->getDatabaseIdentifierValue($this->uow->getDocumentIdentifier($document));
     $query = array('_id' => $id);
     if ($class->isVersioned) {
         $query[$class->versionField] = $class->reflFields[$class->versionField]->getValue($document);
     }
     $collection = $this->dm->getDocumentCollection($className);
     $result = $collection->update($query, $newObj, $options);
     if ($class->isVersioned && !$result['n']) {
         throw LockException::lockFailed($document);
     }
 }