yii2.0获取到最后一条执行的sql怎么看

yii2.0获取到最后一条执行的sql怎么看


POPMUISE
浏览 595回答 1
1回答

智慧大石

这个有很多种方法1. yii有提供一个 getRawSql方法  比如说一个查询1234$query = User::find();$query->select(['username','age'])->where(['id'=>1)->one(); echo $query->createCommand()->getRawSql();//输出sql语句2.可开启yii2的debug模块,这个功能很强大,在里面可以查到当前页面所有的sql信息,具体配置方法自行百度,网上太多这个配置了3.查找Yii源码   随便找个模型调用原生的方法 比如 User::updateAll 方法,通过编辑器定位到updateAll方法的源码 你会发现下面一段代码1234567public static function updateAll($attributes, $condition = '', $params = []){   $command = static::getDb()->createCommand();   $command->update(static::tableName(), $attributes, $condition, $params);    return $command->execute();}继续定位execute方法12345678910111213141516171819202122232425262728public function execute(){   $sql = $this->getSql();   $rawSql = $this->getRawSql();    Yii::info($rawSql, __METHOD__);   if ($sql == '') {      return 0;   }    $this->prepare(false);        $token = $rawSql;        try {            Yii::beginProfile($token, __METHOD__);             $this->pdoStatement->execute();            $n = $this->pdoStatement->rowCount();             Yii::endProfile($token, __METHOD__);             $this->refreshTableSchema();             return $n;        } catch (\Exception $e) {            Yii::endProfile($token, __METHOD__);            throw $this->db->getSchema()->convertException($e, $rawSql);        }    }方法里 $rawSql就是最原生要执行的sql拉,在这里打断点输出就ok  
打开App,查看更多内容
随时随地看视频慕课网APP