Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/BatchQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
30 changes: 16 additions & 14 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,31 @@ 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, $indexBy = null, $db = null)
{
$db = $db === null ? yii::$app->mongodb : $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);
}
}
Expand Down Expand Up @@ -322,8 +328,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, $this->indexBy, $db);
return $this->populate($rows);
}

Expand Down Expand Up @@ -358,8 +363,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);
}

/**
Expand All @@ -384,8 +388,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;
Expand Down Expand Up @@ -417,8 +420,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 [];
Expand Down