Class yii\mongodb\debug\ExplainAction

Inheritanceyii\mongodb\debug\ExplainAction » yii\base\Action
Available since extension's version2.0.5
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/debug/ExplainAction.php

ExplainAction provides EXPLAIN information for MongoDB queries

Public Properties

Hide inherited properties

Property Type Description Defined By
$panel yii\mongodb\debug\MongoDbPanel Related debug toolbar panel yii\mongodb\debug\ExplainAction

Public Methods

Hide inherited methods

Method Description Defined By
run() Runs the explain action yii\mongodb\debug\ExplainAction

Protected Methods

Hide inherited methods

Method Description Defined By
getCursorFromQueryLog() Create MongoCursor from string query log yii\mongodb\debug\ExplainAction
prepareQuery() Prepare query for using in MongoCursor. yii\mongodb\debug\ExplainAction

Property Details

Hide inherited properties

$panel public property

Related debug toolbar panel

Method Details

Hide inherited methods

getCursorFromQueryLog() protected method

Create MongoCursor from string query log

protected \MongoCursor|null getCursorFromQueryLog ( $queryString )
$queryString string

                protected function getCursorFromQueryLog($queryString)
{
    $cursor = null;
    $connection = $this->panel->getDb();
    $connection->open();
    if ($connection->isActive) {
        $queryInfo = Json::decode($queryString);
        $query = $this->prepareQuery(isset($queryInfo['query']['$query']) ? $queryInfo['query']['$query'] : $queryInfo['query']);
        $cursor = new \MongoCursor($connection->mongoClient, $queryInfo['ns'], $query, $queryInfo['fields']);
        $cursor->limit($queryInfo['limit']);
        $cursor->skip($queryInfo['skip']);
        if (isset($queryInfo['query']['$orderby'])) {
            $cursor->sort($queryInfo['query']['$orderby']);
        }
    }
    return $cursor;
}

            
prepareQuery() protected method

Prepare query for using in MongoCursor.

Converts array contains $id key into MongoId instance. If array given, each element of it will be processed.

protected array|string prepareQuery ( $query )
$query mixed

Raw query

return array|string

Prepared query

                protected function prepareQuery($query)
{
    if (is_array($query)) {
        if (count($query) === 1 && isset($query['$id'])) {
            return new \MongoId($query['$id']);
        } else {
            $result = [];
            foreach ($query as $key => $value) {
                $result[$key] = $this->prepareQuery($value);
            }
            return $result;
        }
    } else {
        return $query;
    }
}

            
run() public method

Runs the explain action

public string run ( $seq, $tag )
$seq integer
$tag string
return string

Explain result content

throws \yii\web\HttpException

if requested log not found

                public function run($seq, $tag)
{
    $this->controller->loadData($tag);
    $timings = $this->panel->calculateTimings();
    if (!isset($timings[$seq])) {
        throw new HttpException(404, 'Log message not found.');
    }
    $query = $timings[$seq]['info'];
    preg_match('/^.+\((.*)\)$/', $query, $matches);
    if (!isset($matches[1])) {
        return '';
    }
    $cursor = $this->getCursorFromQueryLog($matches[1]);
    if (!$cursor) {
        return '';
    }
    $result = $cursor->explain();
    return Json::encode($result, JSON_PRETTY_PRINT);
}