/** * @param \MongoGridFSCursor $cursor Mongo cursor instance to fetch data from. * @param boolean $all whether to fetch all rows or only first one. * @param string|callable $indexBy value to index by. * @return array|boolean result. * @see Query::fetchRows() */ protected function fetchRowsInternal($cursor, $all, $indexBy) { $result = []; if ($all) { foreach ($cursor as $file) { $row = $file->file; $row['file'] = $file; if ($indexBy !== null) { if (is_string($indexBy)) { $key = $row[$indexBy]; } else { $key = call_user_func($indexBy, $row); } $result[$key] = $row; } else { $result[] = $row; } } } else { if ($cursor->hasNext()) { $file = $cursor->getNext(); $result = $file->file; $result['file'] = $file; } else { $result = false; } } return $result; }
public function __construct($gridFsName, $filter = array()) { $databases = Conf::f('databases', array()); $config = Conf::f('storage-mongodb-object', array()); $dbName = isset($config['databases']) ? $config['databases'] : 'default'; if (!isset($databases[$dbName])) { throw new \photon\db\UndefinedConnection(sprintf('The connection "%s" is not defined in the configuration.', $dbName)); } $class = class_exists('\\MongoClient') ? '\\MongoClient' : '\\Mongo'; $conn = new $class($databases[$dbName]['server'], $databases[$dbName]['options']); $db = $conn->selectDB($databases[$dbName]['database']); $gridfs = $db->getGridFS($gridFsName); $ns = $databases[$dbName]['database'] . '.' . $gridFsName . '.files'; $fields = array(); parent::__construct($gridfs, $conn, $ns, $filter, $fields); }
/** * @link http://php.net/manual/en/mongogridfs.find.php * @param array $query The query * @param array $fields Fields to return * @param array $options Options for the find command * @return MongoGridFSCursor A MongoGridFSCursor */ public function find(array $query = [], array $fields = []) { $cursor = new MongoGridFSCursor($this, $this->db->getConnection(), (string) $this, $query, $fields); $cursor->setReadPreference($this->getReadPreference()); return $cursor; }
public function count($foundOnly = false) { $this->time->start(); $return = parent::count($foundOnly); $time = $this->time->stop(); $info = $this->info(); $this->log(array('type' => 'count', 'query' => is_array($info['query']) ? $info['query'] : array(), 'limit' => $info['limit'], 'skip' => $info['skip'], 'foundOnly' => $foundOnly, 'time' => $time)); return $return; }