public function testDirectMatch() { $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('fs')->where(['file_index' => 5])->all($connection); $this->assertEquals(1, count($rows)); /* @var $file \MongoGridFSFile */ $file = $rows[0]; $this->assertEquals('name5', $file['filename']); }
/** * Executes query and returns a single row of result. * @param \yii\mongodb\Connection $db the Mongo connection used to execute the query. * If null, the Mongo connection returned by [[modelClass]] will be used. * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], * the query result may be either an array or an ActiveRecord object. Null will be returned * if the query results in nothing. */ public function one($db = null) { $row = parent::one($db); if ($row !== false) { if ($this->asArray) { $model = $row; } else { /** @var ActiveRecord $class */ $class = $this->modelClass; $model = $class::instantiate($row); $class::populateRecord($model, $row); } if (!empty($this->with)) { $models = [$model]; $this->findWith($this->with, $models); $model = $models[0]; } if (!$this->asArray) { $model->afterFind(); } return $model; } else { return null; } }
public function get($user_id) { $query = new Query(); $row = $query->from('photo_profile')->where(['userId' => $user_id])->one($this->db); return ['contentType' => $row['contentType'], 'byte' => $row['file']->getBytes()]; }
/** * Converts the raw query results into the format as specified by this query. * This method is internally used to convert the data fetched from MongoDB * into the format as required by this query. * @param array $rows the raw query result from MongoDB * @return array the converted query result */ public function populate($rows) { if (empty($rows)) { return []; } $indexBy = $this->indexBy; $this->indexBy = null; $rows = parent::populate($rows); $this->indexBy = $indexBy; $models = $this->createModels($rows); if (!empty($this->with)) { $this->findWith($this->with, $models); } if (!$this->asArray) { foreach ($models as $model) { $model->afterFind(); } } return $models; }