yii\db\Command::getRawSql PHP 메소드

getRawSql() 공개 메소드

Note that the return value of this method should mainly be used for logging purpose. It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders.
public getRawSql ( ) : string
리턴 string the raw SQL with parameter values inserted into the corresponding placeholders in [[sql]].
    public function getRawSql()
    {
        if (empty($this->params)) {
            return $this->_sql;
        }
        $params = [];
        foreach ($this->params as $name => $value) {
            if (is_string($name) && strncmp(':', $name, 1)) {
                $name = ':' . $name;
            }
            if (is_string($value)) {
                $params[$name] = $this->db->quoteValue($value);
            } elseif (is_bool($value)) {
                $params[$name] = $value ? 'TRUE' : 'FALSE';
            } elseif ($value === null) {
                $params[$name] = 'NULL';
            } elseif (!is_object($value) && !is_resource($value)) {
                $params[$name] = $value;
            }
        }
        if (!isset($params[1])) {
            return strtr($this->_sql, $params);
        }
        $sql = '';
        foreach (explode('?', $this->_sql) as $i => $part) {
            $sql .= (isset($params[$i]) ? $params[$i] : '') . $part;
        }
        return $sql;
    }

Usage Example

예제 #1
0
 /**
  * Extended functional: caching rawSql data between queryInternal() calls for one query execution
  * @inheritdoc
  */
 public function getRawSql()
 {
     if ($rawSql = static::$requestLocalCache->get('rawSql')) {
         static::$requestLocalCache->delete('rawSql');
         return $rawSql;
     }
     return parent::getRawSql();
 }