Collection.php 6.48 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\mongodb\file;
9

10
use yii\mongodb\Exception;
11
use Yii;
12 13 14 15

/**
 * Collection represents the Mongo GridFS collection information.
 *
16 17 18 19
 * A file collection object is usually created by calling [[Database::getFileCollection()]] or [[Connection::getFileCollection()]].
 *
 * File collection inherits all interface from regular [[\yii\mongo\Collection]], adding methods to store files.
 *
20
 * @property \yii\mongo\Collection $chunkCollection file chunks Mongo collection. This property is read-only.
21 22 23 24 25
 * @method \MongoGridFSCursor find() returns a cursor for the search results.
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
26
class Collection extends \yii\mongodb\Collection
27 28 29 30 31
{
	/**
	 * @var \MongoGridFS Mongo GridFS collection instance.
	 */
	public $mongoCollection;
32
	/**
33
	 * @var \yii\mongodb\Collection file chunks Mongo collection.
34 35 36 37 38 39
	 */
	private $_chunkCollection;

	/**
	 * Returns the Mongo collection for the file chunks.
	 * @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
40
	 * @return \yii\mongodb\Collection mongo collection instance.
41 42 43 44 45
	 */
	public function getChunkCollection($refresh = false)
	{
		if ($refresh || !is_object($this->_chunkCollection)) {
			$this->_chunkCollection = Yii::createObject([
46
				'class' => 'yii\mongodb\Collection',
47 48 49 50 51
				'mongoCollection' => $this->mongoCollection->chunks
			]);
		}
		return $this->_chunkCollection;
	}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

	/**
	 * Removes data from the collection.
	 * @param array $condition description of records to remove.
	 * @param array $options list of options in format: optionName => optionValue.
	 * @return integer|boolean number of updated documents or whether operation was successful.
	 * @throws Exception on failure.
	 */
	public function remove($condition = [], $options = [])
	{
		$result = parent::remove($condition, $options);
		$this->tryLastError(); // MongoGridFS::remove will return even if the remove failed
		return $result;
	}

	/**
68 69
	 * Creates new file in GridFS collection from given local filesystem file.
	 * Additional attributes can be added file document using $metadata.
70 71 72 73 74
	 * @param string $filename name of the file to store.
	 * @param array $metadata other metadata fields to include in the file document.
	 * @param array $options list of options in format: optionName => optionValue
	 * @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
	 * unless an "_id" was explicitly specified in the metadata.
75
	 * @throws Exception on failure.
76
	 */
77
	public function insertFile($filename, $metadata = [], $options = [])
78
	{
79 80 81 82 83 84 85 86 87 88 89 90
		$token = 'Inserting file into ' . $this->getFullName();
		Yii::info($token, __METHOD__);
		try {
			Yii::beginProfile($token, __METHOD__);
			$options = array_merge(['w' => 1], $options);
			$result = $this->mongoCollection->storeFile($filename, $metadata, $options);
			Yii::endProfile($token, __METHOD__);
			return $result;
		} catch (\Exception $e) {
			Yii::endProfile($token, __METHOD__);
			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
		}
91 92 93
	}

	/**
94 95
	 * Creates new file in GridFS collection with specified content.
	 * Additional attributes can be added file document using $metadata.
96
	 * @param string $bytes string of bytes to store.
97 98 99 100
	 * @param array $metadata other metadata fields to include in the file document.
	 * @param array $options list of options in format: optionName => optionValue
	 * @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
	 * unless an "_id" was explicitly specified in the metadata.
101
	 * @throws Exception on failure.
102
	 */
103
	public function insertFileContent($bytes, $metadata = [], $options = [])
104
	{
105 106 107 108 109 110 111 112 113 114 115 116
		$token = 'Inserting file content into ' . $this->getFullName();
		Yii::info($token, __METHOD__);
		try {
			Yii::beginProfile($token, __METHOD__);
			$options = array_merge(['w' => 1], $options);
			$result = $this->mongoCollection->storeBytes($bytes, $metadata, $options);
			Yii::endProfile($token, __METHOD__);
			return $result;
		} catch (\Exception $e) {
			Yii::endProfile($token, __METHOD__);
			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
		}
117 118 119
	}

	/**
120 121
	 * Creates new file in GridFS collection from uploaded file.
	 * Additional attributes can be added file document using $metadata.
122 123 124 125 126
	 * @param string $name name of the uploaded file to store. This should correspond to
	 * the file field's name attribute in the HTML form.
	 * @param array $metadata other metadata fields to include in the file document.
	 * @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
	 * unless an "_id" was explicitly specified in the metadata.
127
	 * @throws Exception on failure.
128
	 */
129
	public function insertUploads($name, $metadata = [])
130
	{
131 132 133 134 135 136 137 138 139 140 141
		$token = 'Inserting file uploads into ' . $this->getFullName();
		Yii::info($token, __METHOD__);
		try {
			Yii::beginProfile($token, __METHOD__);
			$result = $this->mongoCollection->storeUpload($name, $metadata);
			Yii::endProfile($token, __METHOD__);
			return $result;
		} catch (\Exception $e) {
			Yii::endProfile($token, __METHOD__);
			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
		}
142 143 144
	}

	/**
145
	 * Retrieves the file with given _id.
146 147
	 * @param mixed $id _id of the file to find.
	 * @return \MongoGridFSFile|null found file, or null if file does not exist
148
	 * @throws Exception on failure.
149 150 151
	 */
	public function get($id)
	{
152 153 154 155 156 157 158 159 160 161 162
		$token = 'Inserting file uploads into ' . $this->getFullName();
		Yii::info($token, __METHOD__);
		try {
			Yii::beginProfile($token, __METHOD__);
			$result = $this->mongoCollection->get($id);
			Yii::endProfile($token, __METHOD__);
			return $result;
		} catch (\Exception $e) {
			Yii::endProfile($token, __METHOD__);
			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
		}
163 164 165
	}

	/**
166
	 * Deletes the file with given _id.
167 168
	 * @param mixed $id _id of the file to find.
	 * @return boolean whether the operation was successful.
169
	 * @throws Exception on failure.
170 171 172
	 */
	public function delete($id)
	{
173 174 175 176 177 178 179 180 181 182 183 184
		$token = 'Inserting file uploads into ' . $this->getFullName();
		Yii::info($token, __METHOD__);
		try {
			Yii::beginProfile($token, __METHOD__);
			$result = $this->mongoCollection->delete($id);
			$this->tryResultError($result);
			Yii::endProfile($token, __METHOD__);
			return true;
		} catch (\Exception $e) {
			Yii::endProfile($token, __METHOD__);
			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
		}
185 186
	}
}