yii2.0獲取到最后一條執行的sql怎么看
1 回答
holdtom
TA貢獻1805條經驗 獲得超10個贊
這個有很多種方法
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方法的源碼 你會發現下面一段代碼
| 1234567 | public static function updateAll($attributes, $condition = '', $params = []){ $command = static::getDb()->createCommand(); $command->update(static::tableName(), $attributes, $condition, $params); return $command->execute();} |
繼續定位execute方法
| 12345678910111213141516171819202122232425262728 | public 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
個人推薦第二種方法,最方法最高效,具體配置方法自己百度,很簡單!
- 1 回答
- 0 關注
- 1680 瀏覽
添加回答
舉報
0/150
提交
取消
