findOne() public method

Querys this collection, returning a single element
public findOne ( array $query = [], array $fields = [], array $options = [] ) : array | null
$query array The fields for which to search.
$fields array Fields of the results to return.
$options array
return array | null
Beispiel #1
0
 public function findOne($email)
 {
     $one = $this->contactsManagerCollection->findOne(array('email' => $email));
     if ($one != null) {
         return $this->createContact($one);
     }
 }
 /**
  * Get data by key if not expired
  * @param $key
  * @return string
  */
 protected function get($key)
 {
     $record = $this->mongoCollection->findOne(array('key' => $key));
     if ($record && is_array($record) && array_key_exists('data', $record)) {
         return $record['data'];
     }
 }
 /**
  * Retrieve a log entry by his ID.
  *
  * @param string $id
  *
  * @return Log|null
  */
 public function getLogById($id)
 {
     $log = $this->collection->findOne(array('_id' => array('$eq' => new \MongoId($id))));
     if (!$log) {
         return null;
     }
     return $this->convertArrayToEntity($log);
 }
 /**
  * {@inheritdoc}
  */
 public function findByEmail(EmailAddress $email)
 {
     $persistedState = $this->collection->findOne(['email' => (string) $email]);
     if ($persistedState === null) {
         return null;
     }
     return AccountCredentials::fromPersistedState($persistedState);
 }
Beispiel #5
0
 /**
  * Read Session Data
  *
  * @param string $id
  *   Session Data ID
  *
  * @return string|NULL
  *
  * If the fetching process was successfully completed and read data
  * is not corrupted, a string will be returned.
  *
  * Otherwise, NULL will
  */
 public function read($id)
 {
     $data = $this->collection->findOne(array('_id' => $id), array('serialized'));
     if (isset($data['serialized']) && $data['serialized'] instanceof \MongoBinData) {
         return gzuncompress($data['serialized']->bin);
     }
     return NULL;
 }
Beispiel #6
0
 /**
  */
 public function exists($tokenID)
 {
     try {
         return !is_null($this->_db->findOne(array(self::ADDRESS => $this->_encodeRemoteAddress(), self::TID => $tokenID)));
     } catch (MongoException $e) {
         return false;
     }
 }
 /**
  * Retrieve the cached results of the api $request.
  *
  * @param RequestInterface $request A request for which the response may be cached.
  *
  * @return ResponseInterface|null
  */
 public function get(RequestInterface $request)
 {
     $cached = $this->collection->findOne(['_id' => $request->getUrl()]);
     if ($cached === null) {
         return null;
     }
     return new Response($cached['httpCode'], $cached['headers'], $cached['body']);
 }
Beispiel #8
0
 /**
  * Retrieves the cache item for the given id.
  *
  * @param  string     $key The cache key to retrieve.
  * @return mixed|null Returns the cached data or null.
  */
 public function get($key)
 {
     $cache = $this->collection->findOne(array('key' => $key), array('data', 'expire'));
     if ($cache !== null) {
         $this->ttls[$key] = isset($cache['expire']) ? $cache['expire']->sec - time() : 0;
     }
     return $cache;
 }
Beispiel #9
0
 /**
  */
 public function getLockInfo($lockid)
 {
     $query = array(self::LID => $lockid, '$or' => array(array(self::EXPIRY_TS => array('$gte' => time())), array(self::EXPIRY_TS => Horde_Lock::PERMANENT)));
     try {
         return $this->_mapFields($this->_db->findOne($query));
     } catch (MongoException $e) {
         throw new Horde_Lock_Exception($e);
     }
 }
Beispiel #10
0
 protected function getGroup($identifier)
 {
     $res = $this->collection->findOne([self::FIELD_TYPE => self::RECORD_TYPE_KEYWORD, self::FIELD_IDENTIFIER => $identifier]);
     if ($res) {
         return $res[self::FIELD_GROUP];
     } else {
         throw new \RuntimeException('Could not get group name. Identifier is not subscribed');
     }
 }
Beispiel #11
0
 public function findOne($criteria)
 {
     $result = null;
     $document = $this->mongoCollection->findOne($criteria);
     if ($document) {
         $result = $this->objectFactory->unmarshal($document);
     }
     return $result;
 }
 public function search($query)
 {
     $results = [];
     $data = $this->index->findOne(array('token' => $query));
     if (!isset($data)) {
         return $results;
     }
     return $data['documents'];
 }
Beispiel #13
0
 public function insertOrUpdate($product)
 {
     $result = $this->collection->findOne(['product_id' => $product['product_id']]);
     if ($result) {
         $this->update($product);
     } else {
         $this->insert($product);
     }
 }
Beispiel #14
0
 /**
  * {@inheritDoc}
  */
 public function isCached($providerName, $query)
 {
     $result = $this->collection->findOne(array('id' => $this->getKey($providerName, $query)));
     if (null === $result) {
         return false;
     }
     $cached = new BatchGeocoded();
     $cached->fromArray($result);
     return $cached;
 }
Beispiel #15
0
 /**
  * {@inheritdoc }
  */
 public function get($key)
 {
     $tKey = $this->getKey($key);
     $tNow = $this->getTtl();
     $data = $this->collection->findOne(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow)));
     if (isset($data)) {
         return $this->unPack($data['value']);
     }
     return false;
 }
 public function testWrite()
 {
     $id = "somekey";
     $value = "Foo";
     $this->assertEquals(null, $this->collection->findOne());
     $cache = SS_Cache::factory(self::$cache_name);
     /** @var ICacheFrontend$cache */
     $cache->save($value, $id);
     $this->assertEquals(1, $this->collection->find()->count());
     $this->assertEquals($value, $this->collection->find()->getNext()['content']);
 }
 /**
  * {@inheritdoc}
  */
 public function read($sessionId)
 {
     $record = $this->collection->findOne(['_id' => $this->encodeSessionId($sessionId)]);
     if ($record == null) {
         return '';
     }
     if ($record['expireat']->sec < time()) {
         return '';
     }
     return serialize($record['data']);
 }
Beispiel #18
0
 /**
  * {@inheritdoc}
  */
 public function getUpdatedAt()
 {
     $info = $this->collection->findOne();
     if ($info && array_key_exists('updatedAt', $info)) {
         $mongoDate = $info['updatedAt'];
         $updatedAt = new \DateTime();
         $updatedAt->setTimestamp($mongoDate->sec);
         return $updatedAt;
     }
     return null;
 }
Beispiel #19
0
 /**
  * {@inheritdoc}
  */
 public function loadKey($key)
 {
     $cache = $this->collection->findOne(array('key' => $this->mapKey($key)), array('data', 'expire'));
     // check expiration
     if (isset($cache['expire']) && (string) $cache['expire'] < time()) {
         return null;
     }
     if (null !== $this->serializer && $this->serializer->isSerialized($cache['data'])) {
         return $this->serializer->unserialize($cache['data']);
     }
     return $cache['data'];
 }
Beispiel #20
0
 public function getRow($select)
 {
     if (!$select) {
         throw new Kwf_Exception('getRow needs a parameter, null is not allowed.');
     }
     if (!is_object($select)) {
         $select = $this->select($select);
     }
     $profiler = Kwf_Registry::get('db')->getProfiler();
     $p = $profiler->queryStart($this->_collection->__toString() . "\n" . Zend_Json::encode($this->_getQuery($select)));
     $row = $this->_collection->findOne($this->_getQuery($select));
     $p = $profiler->queryEnd($p);
     if (!$row) {
         return null;
     }
     $id = $row['_id'];
     if ($id instanceof MongoId) {
         $id = $id->__toString();
     }
     if (!isset($this->_data[$id])) {
         $this->_data[$id] = $row;
     }
     $ret = new $this->_rowClass(array('data' => $this->_data[$id], 'model' => $this));
     return $ret;
 }
Beispiel #21
0
 /**
  */
 public function exists($key, $lifetime = 0)
 {
     $okey = $key;
     $key = $this->_getCid($key);
     /* Build SQL query. */
     $query = array(self::CID => $key);
     // 0 lifetime checks for objects which have no expiration
     if ($lifetime != 0) {
         $query[self::TIMESTAMP] = array('$gte' => time() - $lifetime);
     }
     try {
         $result = $this->_db->findOne($query);
     } catch (MongoException $e) {
         $this->_logger->log($e->getMessage(), 'DEBUG');
         return false;
     }
     if (is_null($result)) {
         if ($this->_logger) {
             $this->_logger->log(sprintf('Cache exists() miss: %s (cache ID %s)', $okey, $key), 'DEBUG');
         }
         return false;
     }
     if ($this->_logger) {
         $this->_logger->log(sprintf('Cache exists() hit: %s (cache ID %s)', $okey, $key), 'DEBUG');
     }
     return true;
 }
Beispiel #22
0
 /**
  * {@inheritDoc}
  */
 public function findOne(array $query = array())
 {
     $found = $this->collection->findOne($query);
     if (is_array($found)) {
         return $this->createFromDb($found);
     }
 }
Beispiel #23
0
 /**
  * Internal method to get an item.
  *
  * @param  string  $normalizedKey
  * @param  bool $success
  * @param  mixed   $casToken
  * @return mixed Data on success, null on failure
  * @throws Exception\ExceptionInterface
  */
 protected function internalGetItem(&$normalizedKey, &$success = null, &$casToken = null)
 {
     try {
         $this->initMongo();
         $data = $this->collection->findOne(array('ns' => $this->namespace, 'key' => $normalizedKey));
         $current = new \MongoDate();
         $success = true;
         if (empty($data)) {
             $success = false;
             return $data;
         } elseif (is_array($data) && $data['ttl'] > 0 && $current->sec - $data['created']->sec > $data['ttl']) {
             $this->getEventManager()->trigger(new Storage\PostEvent('onCacheItem.expired.ttl', $this, new \ArrayObject(func_get_args()), $data));
             $data = null;
             //force expire
             $success = false;
         }
         if ($success && isset($data['data']) && is_object($data['data']) && $data['data'] instanceof \MongoBinData) {
             $data['data'] = unserialize($data['data']->bin);
         }
         return $data;
     } catch (\MongoException $e) {
         if ($this->throwExceptions) {
             throw $e;
         }
         $success = false;
         return null;
     }
 }
 /**
  * This writes to memory. 
  * After returning PHP will invoke SessionHandler::close.
  * 
  * @param string $sessionId
  * @param string $data Serialized shit
  * @return boolean
  */
 public function write($sessionId, $data)
 {
     $query = array("session-id" => $sessionId);
     $toSave = array_merge($query, array("data" => $data, "time" => time()));
     try {
         $el = $this->collection->findOne($query);
         if ($el === null) {
             $result = $this->collection->save($toSave);
         } else {
             $result = $this->collection->update($query, $toSave);
         }
         return $result["ok"] == 1;
     } catch (MongoCursorException $ex) {
         return false;
     }
 }
Beispiel #25
0
 /**
  * Fetches the object pointed to by a reference
  *
  * @param mongodb $db - Database to use.
  * @param array $ref - Reference to fetch.
  *
  * @return array - Returns the document to which the reference refers
  *   or NULL if the document does not exist (the reference is broken).
  */
 public static function get(MongoDB $db, $ref)
 {
     $ref = (array) $ref;
     if (!isset($ref['$id']) || !isset($ref['$ref'])) {
         return;
     }
     $ns = $ref['$ref'];
     $id = $ref['$id'];
     $refdb = null;
     if (isset($ref['$db'])) {
         $refdb = $ref['$db'];
     }
     if (!is_string($ns)) {
         throw new MongoException('MongoDBRef::get: $ref field must be a string', 10);
     }
     if (isset($refdb)) {
         if (!is_string($refdb)) {
             throw new MongoException('MongoDBRef::get: $db field of $ref must be a string', 11);
         }
         if ($refdb != (string) $db) {
             $db = $db->_getClient()->{$refdb};
         }
     }
     $collection = new MongoCollection($db, $ns);
     $query = ['_id' => $id];
     return $collection->findOne($query);
 }
 /**
  * Find a single document with the given query and select fields.
  *
  * @param string $documentName The document to find.
  * @param array $query The query criteria.
  * @param array $select The fields to select
  * @return object $document
  */
 public function findOne(array $query = array(), array $select = array())
 {
     $result = $this->_collection->findOne($query, $select);
     if ($result !== null) {
         return $this->_unitOfWork->getOrCreateDocument($this->_documentName, $result);
     }
     return null;
 }
 /**
  * Sets the lastMessageDate timestamp on the thread.
  *
  * @param array &$thread
  */
 private function createThreadLastMessageDate(array &$thread)
 {
     $lastMessageRef = end($thread['messages']);
     if (false !== $lastMessageRef) {
         $lastMessage = $this->messageCollection->findOne(array('_id' => $lastMessageRef['$id']), array('createdAt' => 1));
     }
     $thread['lastMessageDate'] = isset($lastMessage['createdAt']) ? $lastMessage['createdAt'] : null;
 }
Beispiel #28
0
 /**
  * Lookup a specific cache entry
  * 
  * Optionally, increment the hit counter when loading the cache entry
  * 
  * @param integer $id
  * @param boolean $incrementHitCounter = false
  * @return array | FALSE
  */
 private function get($id, $incrementHitCounter = false)
 {
     if ($incrementHitCounter === true) {
         return $this->_collection->findAndModify(array('_id' => $id), array('$inc' => array('hits' => 1)));
     } else {
         return $this->_collection->findOne(array('_id' => $id));
     }
 }
Beispiel #29
0
 /**
  * {@inheritDoc}
  */
 public function find($storageName, $key)
 {
     $this->initialize();
     $value = $this->collection->findOne(['key' => $key], ['value']);
     if ($value) {
         return $value['value'];
     }
     throw new NotFoundException();
 }
Beispiel #30
0
 function findOne($query = array(), $fields = array())
 {
     br()->log()->writeln('MONGO->FIND', "QRY");
     br()->log()->writeln($query, "FLT");
     br()->log()->writeln($fields, "OPT");
     $result = parent::findOne($query, $fields);
     br()->log()->writeln('Query complete', 'SEP');
     return $result;
 }