You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					73 lines
				
				1.7 KiB
			
		
		
			
		
	
	
					73 lines
				
				1.7 KiB
			| 
											12 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace yii\mongodb\file;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | use Yii;
 | ||
|  | 
 | ||
|  | /**
 | ||
| 
											12 years ago
										 |  * Query represents Mongo "find" operation for GridFS collection.
 | ||
|  |  *
 | ||
| 
											12 years ago
										 |  * Query behaves exactly as regular [[\yii\mongodb\Query]].
 | ||
| 
											12 years ago
										 |  * Found files will be represented as arrays of file document attributes with
 | ||
|  |  * additional 'file' key, which stores [[\MongoGridFSFile]] instance.
 | ||
| 
											12 years ago
										 |  *
 | ||
|  |  * @author Paul Klimov <klimov.paul@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											12 years ago
										 | class Query extends \yii\mongodb\Query
 | ||
| 
											12 years ago
										 | {
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the Mongo collection for this query.
 | ||
| 
											12 years ago
										 | 	 * @param \yii\mongodb\Connection $db Mongo connection.
 | ||
| 
											12 years ago
										 | 	 * @return Collection collection instance.
 | ||
|  | 	 */
 | ||
|  | 	public function getCollection($db = null)
 | ||
|  | 	{
 | ||
|  | 		if ($db === null) {
 | ||
| 
											12 years ago
										 | 			$db = Yii::$app->getComponent('mongodb');
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		return $db->getFileCollection($this->from);
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @param \MongoGridFSCursor $cursor Mongo cursor instance to fetch data from.
 | ||
| 
											12 years ago
										 | 	 * @param boolean $all whether to fetch all rows or only first one.
 | ||
| 
											12 years ago
										 | 	 * @param string|callable $indexBy value to index by.
 | ||
| 
											12 years ago
										 | 	 * @return array|boolean result.
 | ||
| 
											12 years ago
										 | 	 * @see Query::fetchRows()
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	protected function fetchRowsInternal($cursor, $all, $indexBy)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$result = [];
 | ||
|  | 		if ($all) {
 | ||
|  | 			foreach ($cursor as $file) {
 | ||
|  | 				$row = $file->file;
 | ||
|  | 				$row['file'] = $file;
 | ||
|  | 				if ($indexBy !== null) {
 | ||
|  | 					if (is_string($indexBy)) {
 | ||
|  | 						$key = $row[$indexBy];
 | ||
| 
											12 years ago
										 | 					} else {
 | ||
| 
											12 years ago
										 | 						$key = call_user_func($indexBy, $row);
 | ||
| 
											12 years ago
										 | 					}
 | ||
| 
											12 years ago
										 | 					$result[$key] = $row;
 | ||
| 
											12 years ago
										 | 				} else {
 | ||
| 
											12 years ago
										 | 					$result[] = $row;
 | ||
| 
											12 years ago
										 | 				}
 | ||
|  | 			}
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
|  | 			if ($cursor->hasNext()) {
 | ||
|  | 				$file = $cursor->getNext();
 | ||
|  | 				$result = $file->file;
 | ||
|  | 				$result['file'] = $file;
 | ||
|  | 			} else {
 | ||
|  | 				$result = false;
 | ||
|  | 			}
 | ||
| 
											12 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 		return $result;
 | ||
| 
											12 years ago
										 | 	}
 | ||
| 
											12 years ago
										 | }
 |