yii\db\Command::queryInternal PHP Method

queryInternal() protected method

Performs the actual DB query of a SQL statement.
Since: 2.0.1 this method is protected (was private before).
protected queryInternal ( string $method, integer $fetchMode = null ) : mixed
$method string method of PDOStatement to be called
$fetchMode integer the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used.
return mixed the method execution result
    protected function queryInternal($method, $fetchMode = null)
    {
        $rawSql = $this->getRawSql();
        Yii::info($rawSql, 'yii\\db\\Command::query');
        if ($method !== '') {
            $info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency);
            if (is_array($info)) {
                /* @var $cache \yii\caching\Cache */
                $cache = $info[0];
                $cacheKey = [__CLASS__, $method, $fetchMode, $this->db->dsn, $this->db->username, $rawSql];
                $result = $cache->get($cacheKey);
                if (is_array($result) && isset($result[0])) {
                    Yii::trace('Query result served from cache', 'yii\\db\\Command::query');
                    return $result[0];
                }
            }
        }
        $this->prepare(true);
        $token = $rawSql;
        try {
            Yii::beginProfile($token, 'yii\\db\\Command::query');
            $this->pdoStatement->execute();
            if ($method === '') {
                $result = new DataReader($this);
            } else {
                if ($fetchMode === null) {
                    $fetchMode = $this->fetchMode;
                }
                $result = call_user_func_array([$this->pdoStatement, $method], (array) $fetchMode);
                $this->pdoStatement->closeCursor();
            }
            Yii::endProfile($token, 'yii\\db\\Command::query');
        } catch (\Exception $e) {
            Yii::endProfile($token, 'yii\\db\\Command::query');
            throw $this->db->getSchema()->convertException($e, $rawSql);
        }
        if (isset($cache, $cacheKey, $info)) {
            $cache->set($cacheKey, [$result], $info[1], $info[2]);
            Yii::trace('Saved query result in cache', 'yii\\db\\Command::query');
        }
        return $result;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Actual query with caching results in local for current request
  * @inheritdoc
  */
 protected function queryInternal($method, $fetchMode = null)
 {
     if ($method !== '') {
         $rawSql = $this->getRawSql();
         $requestLocalCacheKey = implode('', [__CLASS__, $method, $fetchMode, $this->db->dsn, $this->db->username, preg_replace('/\\s+/', '', $rawSql)]);
         mb_convert_encoding($requestLocalCacheKey, 'UTF-8', 'UTF-8');
         if (($result = static::$requestLocalCache->get($requestLocalCacheKey)) !== false) {
             Yii::info('Query result served from request local cache' . PHP_EOL . 'Query: ' . VarDumper::dumpAsString($rawSql) . PHP_EOL . 'Result: ' . VarDumper::dumpAsString($result), __METHOD__);
             return $result;
         }
         static::$requestLocalCache->set('rawSql', $rawSql);
     }
     $result = parent::queryInternal($method, $fetchMode);
     if ($method !== '') {
         static::$requestLocalCache->set($requestLocalCacheKey, $result);
     }
     return $result;
 }
All Usage Examples Of yii\db\Command::queryInternal