From f16408227d9ef2f92159f0587c06cdab97f0bc38 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 2 Jul 2020 20:14:11 +0430 Subject: [PATCH 01/16] logging and profiling based on db settings see yii\mongodb\Connection::enableLogging and yii\mongodb\Connection::enableProfiling --- src/Connection.php | 12 ++++++++---- src/Query.php | 29 +++++++++++++++-------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 590f29d82..366534cad 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -349,17 +349,21 @@ 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..15c3c6d45 100644 --- a/src/Query.php +++ b/src/Query.php @@ -203,25 +203,30 @@ 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 yii\mongodb\Connection $db the MongoDB connection used to fetch rows. * @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. * @throws Exception on failure. * @return array|bool result. */ - protected function fetchRows($cursor, $all = true, $indexBy = null) + protected function fetchRows($db, $all = true, $indexBy = null) { + $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); } } @@ -322,8 +327,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($db, true, $this->indexBy); return $this->populate($rows); } @@ -358,8 +362,7 @@ public function one($db = null) if (!empty($this->emulateExecution)) { return false; } - $cursor = $this->buildCursor($db); - return $this->fetchRows($cursor, false); + return $this->fetchRows($db, false); } /** @@ -384,8 +387,7 @@ public function scalar($db = null) $this->select['_id'] = false; } - $cursor = $this->buildCursor($db); - $row = $this->fetchRows($cursor, false); + $row = $this->fetchRows($db, false); if (empty($row)) { return false; @@ -417,8 +419,7 @@ public function column($db = null) $this->select[] = $this->indexBy; } - $cursor = $this->buildCursor($db); - $rows = $this->fetchRows($cursor, true); + $rows = $this->fetchRows($db, true); if (empty($rows)) { return []; From 0830fe1adccab675c24cba41ea899f80981ab6a2 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 2 Jul 2020 20:21:38 +0430 Subject: [PATCH 02/16] Update Query.php --- src/Query.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Query.php b/src/Query.php index 15c3c6d45..81ee54eeb 100644 --- a/src/Query.php +++ b/src/Query.php @@ -212,6 +212,7 @@ public function buildCursor($db = null) */ protected function fetchRows($db, $all = true, $indexBy = null) { + $db = $db === null ? yii::$app->mongodb : $db; $cursor = $this->buildCursor($db); $token = 'fetch cursor id = ' . $cursor->getId(); if($db->enableLogging) From f6f2e5da9e8e7ec5f4181665eac2ec2c9c07363f Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 2 Jul 2020 21:31:58 +0430 Subject: [PATCH 03/16] Update BatchQueryResult.php --- src/BatchQueryResult.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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; From c1dbe3fbad84c2d080691c070939a209f5960f57 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 3 Jul 2020 17:51:23 +0430 Subject: [PATCH 04/16] change position of $db method like another methods --- src/Query.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Query.php b/src/Query.php index 81ee54eeb..1db42ae3e 100644 --- a/src/Query.php +++ b/src/Query.php @@ -203,14 +203,14 @@ public function buildCursor($db = null) /** * Fetches rows from the given Mongo cursor. - * @param yii\mongodb\Connection $db the MongoDB connection used to fetch rows. * @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($db, $all = true, $indexBy = null) + protected function fetchRows($all = true, $indexBy = null, $db = null) { $db = $db === null ? yii::$app->mongodb : $db; $cursor = $this->buildCursor($db); @@ -328,7 +328,7 @@ public function all($db = null) if (!empty($this->emulateExecution)) { return []; } - $rows = $this->fetchRows($db, true, $this->indexBy); + $rows = $this->fetchRows(true, $this->indexBy, $db); return $this->populate($rows); } @@ -363,7 +363,7 @@ public function one($db = null) if (!empty($this->emulateExecution)) { return false; } - return $this->fetchRows($db, false); + return $this->fetchRows(false, $db); } /** @@ -388,7 +388,7 @@ public function scalar($db = null) $this->select['_id'] = false; } - $row = $this->fetchRows($db, false); + $row = $this->fetchRows(false, $db); if (empty($row)) { return false; @@ -420,7 +420,7 @@ public function column($db = null) $this->select[] = $this->indexBy; } - $rows = $this->fetchRows($db, true); + $rows = $this->fetchRows(true, $db); if (empty($rows)) { return []; From 8f1872d6ae3d947e799c8867f22d41095e0f4aba Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:42:31 +0430 Subject: [PATCH 05/16] Update src/Query.php Co-authored-by: Alexander Makarov --- src/Query.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 1db42ae3e..47e3b81dd 100644 --- a/src/Query.php +++ b/src/Query.php @@ -212,7 +212,9 @@ public function buildCursor($db = null) */ protected function fetchRows($all = true, $indexBy = null, $db = null) { - $db = $db === null ? yii::$app->mongodb : $db; + if ($db === null) { + $db = Yii::$app->get('mongodb'); + } $cursor = $this->buildCursor($db); $token = 'fetch cursor id = ' . $cursor->getId(); if($db->enableLogging) From f6e2afb618385bd26878aca326038f1020d92e2f Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:42:42 +0430 Subject: [PATCH 06/16] Update src/Query.php Co-authored-by: Alexander Makarov --- src/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 47e3b81dd..80a2b50ab 100644 --- a/src/Query.php +++ b/src/Query.php @@ -217,8 +217,9 @@ protected function fetchRows($all = true, $indexBy = null, $db = null) } $cursor = $this->buildCursor($db); $token = 'fetch cursor id = ' . $cursor->getId(); - if($db->enableLogging) + if ($db->enableLogging) { Yii::info($token, __METHOD__); + } try { if($db->enableProfiling) Yii::beginProfile($token, __METHOD__); From bb743333858fefe00ab0e449f3c670d04b3789a2 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:42:52 +0430 Subject: [PATCH 07/16] Update src/Query.php Co-authored-by: Alexander Makarov --- src/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 80a2b50ab..352dbdbbb 100644 --- a/src/Query.php +++ b/src/Query.php @@ -221,8 +221,9 @@ protected function fetchRows($all = true, $indexBy = null, $db = null) Yii::info($token, __METHOD__); } try { - if($db->enableProfiling) + if ($db->enableProfiling) { Yii::beginProfile($token, __METHOD__); + } $result = $this->fetchRowsInternal($cursor, $all); if($db->enableProfiling) Yii::endProfile($token, __METHOD__); From 030e0693ce88219c3498e5af63872bdf1a7f4fdd Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:42:59 +0430 Subject: [PATCH 08/16] Update src/Query.php Co-authored-by: Alexander Makarov --- src/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 352dbdbbb..14d4bfc4f 100644 --- a/src/Query.php +++ b/src/Query.php @@ -225,8 +225,9 @@ protected function fetchRows($all = true, $indexBy = null, $db = null) Yii::beginProfile($token, __METHOD__); } $result = $this->fetchRowsInternal($cursor, $all); - if($db->enableProfiling) + if ($db->enableProfiling) { Yii::endProfile($token, __METHOD__); + } return $result; } catch (\Exception $e) { From 5fdb9a00c10af84fb85200082ec94faf1b6e2bbf Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:43:09 +0430 Subject: [PATCH 09/16] Update src/Query.php Co-authored-by: Alexander Makarov --- src/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 14d4bfc4f..7b9086a46 100644 --- a/src/Query.php +++ b/src/Query.php @@ -231,8 +231,9 @@ protected function fetchRows($all = true, $indexBy = null, $db = null) return $result; } catch (\Exception $e) { - if($db->enableProfiling) + if ($db->enableProfiling) { Yii::endProfile($token, __METHOD__); + } throw new Exception($e->getMessage(), (int) $e->getCode(), $e); } } From 05aa05919c97d953c6b028756f3b5cad36bbece8 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:43:19 +0430 Subject: [PATCH 10/16] Update src/Connection.php Co-authored-by: Alexander Makarov --- src/Connection.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index 366534cad..3f0ce967b 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -349,8 +349,9 @@ public function open() } $token = 'Opening MongoDB connection: ' . $this->dsn; try { - if($this->enableLogging) + if ($this->enableLogging) { Yii::trace($token, __METHOD__); + } if($this->enableProfiling) Yii::beginProfile($token, __METHOD__); $options = $this->options; From d9413e736aab170387bb802a6543f09d161ae403 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:43:30 +0430 Subject: [PATCH 11/16] Update src/Connection.php Co-authored-by: Alexander Makarov --- src/Connection.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index 3f0ce967b..dd90d52c9 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -360,8 +360,9 @@ public function open() $this->manager->selectServer($this->manager->getReadPreference()); $this->initConnection(); - if($this->enableProfiling) + if ($this->enableProfiling) { Yii::endProfile($token, __METHOD__); + } } catch (\Exception $e) { if($this->enableProfiling) Yii::endProfile($token, __METHOD__); From ebeddb6fb0b6bf3c59d411765e8acbbd65694cbe Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:43:39 +0430 Subject: [PATCH 12/16] Update src/Connection.php Co-authored-by: Alexander Makarov --- src/Connection.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index dd90d52c9..b4f82cc8d 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -364,8 +364,9 @@ public function open() Yii::endProfile($token, __METHOD__); } } catch (\Exception $e) { - if($this->enableProfiling) + if ($this->enableProfiling) { Yii::endProfile($token, __METHOD__); + } throw new Exception($e->getMessage(), (int) $e->getCode(), $e); } From 1e760fe71da4dec5707df989bdab66ce64039203 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Mon, 6 Jul 2020 19:43:51 +0430 Subject: [PATCH 13/16] Update src/Connection.php Co-authored-by: Alexander Makarov --- src/Connection.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index b4f82cc8d..7b160a183 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -352,8 +352,9 @@ public function open() if ($this->enableLogging) { Yii::trace($token, __METHOD__); } - if($this->enableProfiling) + if ($this->enableProfiling) { Yii::beginProfile($token, __METHOD__); + } $options = $this->options; $this->manager = new Manager($this->dsn, $options, $this->driverOptions); From 793ca716459109d3a5f259bf49e96d28fe138734 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 17 Jul 2020 10:10:13 +0430 Subject: [PATCH 14/16] fix bug --- src/Query.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Query.php b/src/Query.php index 7b9086a46..71c1b583c 100644 --- a/src/Query.php +++ b/src/Query.php @@ -204,13 +204,11 @@ public function buildCursor($db = null) /** * Fetches rows from the given Mongo cursor. * @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($all = true, $indexBy = null, $db = null) + protected function fetchRows($all = true, $db = null) { if ($db === null) { $db = Yii::$app->get('mongodb'); @@ -334,7 +332,7 @@ public function all($db = null) if (!empty($this->emulateExecution)) { return []; } - $rows = $this->fetchRows(true, $this->indexBy, $db); + $rows = $this->fetchRows(true, $db); return $this->populate($rows); } From 4802d62b63ee39f3e8dd10b5805c215a4508ce24 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 2 Oct 2020 19:36:33 +0330 Subject: [PATCH 15/16] fix bug --- src/ActiveQuery.php | 21 ++++++++++++++++----- src/Query.php | 22 ++++++++++++---------- 2 files changed, 28 insertions(+), 15 deletions(-) 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/Query.php b/src/Query.php index 71c1b583c..be7976bfd 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 ? $db : Yii::$app->get('mongodb'); + } /** * 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); } /** @@ -210,9 +214,7 @@ public function buildCursor($db = null) */ protected function fetchRows($all = true, $db = null) { - if ($db === null) { - $db = Yii::$app->get('mongodb'); - } + $db = $this->getDb($db); $cursor = $this->buildCursor($db); $token = 'fetch cursor id = ' . $cursor->getId(); if ($db->enableLogging) { @@ -288,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, ]); } @@ -316,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, ]); } From dd7c5557150b864075474ad433a012d55843629e Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sat, 31 Oct 2020 13:45:10 +0330 Subject: [PATCH 16/16] Update Query.php --- src/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index be7976bfd..353ca8150 100644 --- a/src/Query.php +++ b/src/Query.php @@ -66,7 +66,7 @@ class Query extends Component implements QueryInterface * @return Connection connection instance. */ public function getDb($db = null){ - return $db === null ? $db : Yii::$app->get('mongodb'); + return $db === null ? Yii::$app->get('mongodb') : $db; } /**