Skip to content

Commit 4802d62

Browse files
committed
fix bug
1 parent 793ca71 commit 4802d62

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/ActiveQuery.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ public function init()
9393
$this->trigger(self::EVENT_INIT);
9494
}
9595

96+
/**
97+
* Returns the connection used by this ActiveQuery.
98+
* @param Connection $db Mongo connection.
99+
* @return Connection connection instance.
100+
*/
101+
public function getDb($db = null){
102+
if($db !== null){
103+
return $db;
104+
}
105+
$modelClass = $this->modelClass;
106+
return $modelClass::getDb();
107+
}
108+
96109
/**
97110
* {@inheritdoc}
98111
*/
@@ -183,15 +196,13 @@ public function modify($update, $options = [], $db = null)
183196
public function getCollection($db = null)
184197
{
185198
/* @var $modelClass ActiveRecord */
186-
$modelClass = $this->modelClass;
187-
if ($db === null) {
188-
$db = $modelClass::getDb();
189-
}
199+
190200
if ($this->from === null) {
201+
$modelClass = $this->modelClass;
191202
$this->from = $modelClass::collectionName();
192203
}
193204

194-
return $db->getCollection($this->from);
205+
return $this->getDb()->getCollection($this->from);
195206
}
196207

197208
/**

src/Query.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class Query extends Component implements QueryInterface
6060
*/
6161
public $options = [];
6262

63+
/**
64+
* Returns the connection used by this Query.
65+
* @param Connection $db Mongo connection.
66+
* @return Connection connection instance.
67+
*/
68+
public function getDb($db = null){
69+
return $db === null ? $db : Yii::$app->get('mongodb');
70+
}
6371

6472
/**
6573
* Returns the Mongo collection for this query.
@@ -68,11 +76,7 @@ class Query extends Component implements QueryInterface
6876
*/
6977
public function getCollection($db = null)
7078
{
71-
if ($db === null) {
72-
$db = Yii::$app->get('mongodb');
73-
}
74-
75-
return $db->getCollection($this->from);
79+
return $this->getDb($db)->getCollection($this->from);
7680
}
7781

7882
/**
@@ -210,9 +214,7 @@ public function buildCursor($db = null)
210214
*/
211215
protected function fetchRows($all = true, $db = null)
212216
{
213-
if ($db === null) {
214-
$db = Yii::$app->get('mongodb');
215-
}
217+
$db = $this->getDb($db);
216218
$cursor = $this->buildCursor($db);
217219
$token = 'fetch cursor id = ' . $cursor->getId();
218220
if ($db->enableLogging) {
@@ -288,7 +290,7 @@ public function batch($batchSize = 100, $db = null)
288290
'class' => BatchQueryResult::className(),
289291
'query' => $this,
290292
'batchSize' => $batchSize,
291-
'db' => $db,
293+
'db' => $this->getDb($db),
292294
'each' => false,
293295
]);
294296
}
@@ -316,7 +318,7 @@ public function each($batchSize = 100, $db = null)
316318
'class' => BatchQueryResult::className(),
317319
'query' => $this,
318320
'batchSize' => $batchSize,
319-
'db' => $db,
321+
'db' => $this->getDb($db),
320322
'each' => true,
321323
]);
322324
}

0 commit comments

Comments
 (0)