diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index ada8b7b9e..6f27d45a3 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -93,6 +93,19 @@ public function init() $this->trigger(self::EVENT_INIT); } + /** + * Returns the connection used by this ActiveQuery. + * @param Connection $db Mongo connection. + * @return Connection connection instance. + */ + public function getDb($db = null){ + if($db !== null){ + return $db; + } + $modelClass = $this->modelClass; + return $modelClass::getDb(); + } + /** * {@inheritdoc} */ @@ -183,15 +196,13 @@ public function modify($update, $options = [], $db = null) public function getCollection($db = null) { /* @var $modelClass ActiveRecord */ - $modelClass = $this->modelClass; - if ($db === null) { - $db = $modelClass::getDb(); - } + if ($this->from === null) { + $modelClass = $this->modelClass; $this->from = $modelClass::collectionName(); } - return $db->getCollection($this->from); + return $this->getDb()->getCollection($this->from); } /** diff --git a/src/BatchQueryResult.php b/src/BatchQueryResult.php index 23f268959..d2c7c5c4c 100644 --- a/src/BatchQueryResult.php +++ b/src/BatchQueryResult.php @@ -129,9 +129,12 @@ protected function fetchData() // @see https://jira.mongodb.org/browse/PHP-457 $this->query->addOptions(['batchSize' => $this->batchSize]); } - $cursor = $this->query->buildCursor($this->db); - $token = 'fetch cursor id = ' . $cursor->getId(); - Yii::info($token, __METHOD__); + $db = $this->db === null ? yii::$app->mongodb : $this->db; + $cursor = $this->query->buildCursor($db); + if($db->enableLogging){ + $token = 'fetch cursor id = ' . $cursor->getId(); + Yii::info($token, __METHOD__); + } if ($cursor instanceof \Iterator) { $this->_iterator = $cursor; diff --git a/src/Connection.php b/src/Connection.php index 590f29d82..7b160a183 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -349,17 +349,25 @@ public function open() } $token = 'Opening MongoDB connection: ' . $this->dsn; try { - Yii::trace($token, __METHOD__); - Yii::beginProfile($token, __METHOD__); + if ($this->enableLogging) { + Yii::trace($token, __METHOD__); + } + if ($this->enableProfiling) { + Yii::beginProfile($token, __METHOD__); + } $options = $this->options; $this->manager = new Manager($this->dsn, $options, $this->driverOptions); $this->manager->selectServer($this->manager->getReadPreference()); $this->initConnection(); - Yii::endProfile($token, __METHOD__); + if ($this->enableProfiling) { + Yii::endProfile($token, __METHOD__); + } } catch (\Exception $e) { - Yii::endProfile($token, __METHOD__); + if ($this->enableProfiling) { + Yii::endProfile($token, __METHOD__); + } throw new Exception($e->getMessage(), (int) $e->getCode(), $e); } diff --git a/src/Query.php b/src/Query.php index f1a8c2386..353ca8150 100644 --- a/src/Query.php +++ b/src/Query.php @@ -60,6 +60,14 @@ class Query extends Component implements QueryInterface */ public $options = []; + /** + * Returns the connection used by this Query. + * @param Connection $db Mongo connection. + * @return Connection connection instance. + */ + public function getDb($db = null){ + return $db === null ? Yii::$app->get('mongodb') : $db; + } /** * Returns the Mongo collection for this query. @@ -68,11 +76,7 @@ class Query extends Component implements QueryInterface */ public function getCollection($db = null) { - if ($db === null) { - $db = Yii::$app->get('mongodb'); - } - - return $db->getCollection($this->from); + return $this->getDb($db)->getCollection($this->from); } /** @@ -203,25 +207,33 @@ public function buildCursor($db = null) /** * Fetches rows from the given Mongo cursor. - * @param \MongoDB\Driver\Cursor $cursor Mongo cursor instance to fetch data from. * @param bool $all whether to fetch all rows or only first one. - * @param string|callable $indexBy the column name or PHP callback, - * by which the query results should be indexed by. + * @param yii\mongodb\Connection $db the MongoDB connection used to fetch rows. * @throws Exception on failure. * @return array|bool result. */ - protected function fetchRows($cursor, $all = true, $indexBy = null) + protected function fetchRows($all = true, $db = null) { + $db = $this->getDb($db); + $cursor = $this->buildCursor($db); $token = 'fetch cursor id = ' . $cursor->getId(); - Yii::info($token, __METHOD__); + if ($db->enableLogging) { + Yii::info($token, __METHOD__); + } try { - Yii::beginProfile($token, __METHOD__); + if ($db->enableProfiling) { + Yii::beginProfile($token, __METHOD__); + } $result = $this->fetchRowsInternal($cursor, $all); - Yii::endProfile($token, __METHOD__); + if ($db->enableProfiling) { + Yii::endProfile($token, __METHOD__); + } return $result; } catch (\Exception $e) { - Yii::endProfile($token, __METHOD__); + if ($db->enableProfiling) { + Yii::endProfile($token, __METHOD__); + } throw new Exception($e->getMessage(), (int) $e->getCode(), $e); } } @@ -278,7 +290,7 @@ public function batch($batchSize = 100, $db = null) 'class' => BatchQueryResult::className(), 'query' => $this, 'batchSize' => $batchSize, - 'db' => $db, + 'db' => $this->getDb($db), 'each' => false, ]); } @@ -306,7 +318,7 @@ public function each($batchSize = 100, $db = null) 'class' => BatchQueryResult::className(), 'query' => $this, 'batchSize' => $batchSize, - 'db' => $db, + 'db' => $this->getDb($db), 'each' => true, ]); } @@ -322,8 +334,7 @@ public function all($db = null) if (!empty($this->emulateExecution)) { return []; } - $cursor = $this->buildCursor($db); - $rows = $this->fetchRows($cursor, true, $this->indexBy); + $rows = $this->fetchRows(true, $db); return $this->populate($rows); } @@ -358,8 +369,7 @@ public function one($db = null) if (!empty($this->emulateExecution)) { return false; } - $cursor = $this->buildCursor($db); - return $this->fetchRows($cursor, false); + return $this->fetchRows(false, $db); } /** @@ -384,8 +394,7 @@ public function scalar($db = null) $this->select['_id'] = false; } - $cursor = $this->buildCursor($db); - $row = $this->fetchRows($cursor, false); + $row = $this->fetchRows(false, $db); if (empty($row)) { return false; @@ -417,8 +426,7 @@ public function column($db = null) $this->select[] = $this->indexBy; } - $cursor = $this->buildCursor($db); - $rows = $this->fetchRows($cursor, true); + $rows = $this->fetchRows(true, $db); if (empty($rows)) { return [];