2 回答

TA貢獻1865條經驗 獲得超7個贊
這個有很多種方法
1. yii有提供一個 getRawSql方法 比如說一個查詢
1 2 3 4 | $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方法的源碼 你會發現下面一段代碼
1 2 3 4 5 6 7 | public static function updateAll($attributes, $condition = '', $params = []) { $command = static::getDb()->createCommand(); $command->update(static::tableName(), $attributes, $condition, $params);
return $command->execute(); } |
繼續定位execute方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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
個人推薦第二種方法,最方法最高效,具體配置方法自己百度,很簡單!
- 2 回答
- 0 關注
- 3387 瀏覽
添加回答
舉報