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
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
11 years ago
|
namespace yii\mongodb\file;
|
||
11 years ago
|
|
||
|
use Yii;
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Query represents Mongo "find" operation for GridFS collection.
|
||
|
*
|
||
11 years ago
|
* Query behaves exactly as regular [[\yii\mongodb\Query]].
|
||
11 years ago
|
* Found files will be represented as arrays of file document attributes with
|
||
|
* additional 'file' key, which stores [[\MongoGridFSFile]] instance.
|
||
11 years ago
|
*
|
||
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
11 years ago
|
class Query extends \yii\mongodb\Query
|
||
11 years ago
|
{
|
||
|
/**
|
||
|
* Returns the Mongo collection for this query.
|
||
11 years ago
|
* @param \yii\mongodb\Connection $db Mongo connection.
|
||
11 years ago
|
* @return Collection collection instance.
|
||
|
*/
|
||
|
public function getCollection($db = null)
|
||
|
{
|
||
|
if ($db === null) {
|
||
11 years ago
|
$db = Yii::$app->getComponent('mongodb');
|
||
11 years ago
|
}
|
||
|
return $db->getFileCollection($this->from);
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* @param \MongoGridFSCursor $cursor Mongo cursor instance to fetch data from.
|
||
11 years ago
|
* @param boolean $all whether to fetch all rows or only first one.
|
||
11 years ago
|
* @param string|callable $indexBy value to index by.
|
||
11 years ago
|
* @return array|boolean result.
|
||
11 years ago
|
* @see Query::fetchRows()
|
||
11 years ago
|
*/
|
||
11 years ago
|
protected function fetchRowsInternal($cursor, $all, $indexBy)
|
||
11 years ago
|
{
|
||
11 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];
|
||
11 years ago
|
} else {
|
||
11 years ago
|
$key = call_user_func($indexBy, $row);
|
||
11 years ago
|
}
|
||
11 years ago
|
$result[$key] = $row;
|
||
11 years ago
|
} else {
|
||
11 years ago
|
$result[] = $row;
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
} else {
|
||
|
if ($cursor->hasNext()) {
|
||
|
$file = $cursor->getNext();
|
||
|
$result = $file->file;
|
||
|
$result['file'] = $file;
|
||
|
} else {
|
||
|
$result = false;
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
return $result;
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|