/** * Разворачиваем данные назад из Grid-а в исходный массив * * @param array $record данные содержащие то, что нужно развернуть * @return готовый массив с развёрнутыми данными */ protected function _unserialize($record) { foreach ($this->_settings['fields'] as $field) { $paths = explode('.', $field); $data = $record; foreach ($paths as $path) { if (!empty($data[$path])) { $data = $data[$path]; } else { $data = null; } } if (!empty($data) && $data instanceof MongoId) { // сохраняем содержимое поля в gridfs, а в значение поля записываем id в grid-е $value = @unserialize($this->_Grid->get($data)->getBytes()); if (!empty($value)) { if (count($paths) == 1) { $record[$paths[0]] = $value; } elseif (count($paths) == 2) { $record[$paths[0]][$paths[1]] = $value; } elseif (count($paths) == 3) { $record[$paths[0]][$paths[1]][$paths[2]] = $value; } elseif (count($paths) == 4) { $record[$paths[0]][$paths[1]][$paths[2]][$paths[3]] = $value; } } } } return $record; }
/** * get. */ public function get($id) { $this->time->start(); $return = parent::get($id); $time = $this->time->stop(); $this->log(array('type' => 'get', 'id' => $id, 'time' => $time)); return $return; }
/** * Wrapper method for MongoGridFS::storeFile(). * * This method returns the GridFSFile object, unlike the base MongoGridFS * method, which returns the "_id" field of the saved document. The "_id" * will be set on the $document parameter, which is passed by reference. * * @see http://php.net/manual/en/mongogridfs.storefile.php * @param string|GridFSFile $file String filename or a GridFSFile object * @param array $document * @param array $options * @return GridFSFile */ public function storeFile($file, array &$document, array $options = []) { if (!$file instanceof GridFSFile) { $file = new GridFSFile($file); } $options = isset($options['safe']) ? $this->convertWriteConcern($options) : $options; if ($file->hasUnpersistedFile()) { $id = $this->mongoCollection->storeFile($file->getFilename(), $document, $options); } else { $id = $this->mongoCollection->storeBytes($file->getBytes(), $document, $options); } $document = array_merge(['_id' => $id], $document); $gridFsFile = $this->mongoCollection->get($id); // TODO: Consider throwing exception if file cannot be fetched $file->setMongoGridFSFile($this->mongoCollection->get($id)); return $file; }
/** * 根据ID获取文件内容,二进制 * * @param string $id * @return bytes */ public function getFileFromGridFS($id) { if (!$id instanceof \MongoId) { $id = new \MongoId($id); } $gridfsFile = $this->_fs->get($id); return $gridfsFile->getBytes(); }