問題描述
我正在這樣做:
$students = Student::find()->all();
return $this->render('process', array('students' => $students));
然后在視圖中顯示:
foreach($students as $student)
{
echo $student->name . ', ';
echo $student->getQuizActivitiesCount(); ?> <br /> <?php
}
我想看到正在執行的 sql 查詢.一個學生有很多"測驗活動,查詢執行得很好,但我需要查看原始 SQL.這可能嗎?
i would like to see the sql query being performed. a student "has many" quiz activities, and the query performs perfectly, but i need to see the raw SQL. is this possible?
推薦答案
方法一
使用返回 yiidbActiveQuery
實例的關系,可以直接在代碼中提取原始 SQL 查詢,例如使用 var_dump()
.
With relations that return yiidbActiveQuery
instance it's possible to extract the raw SQL query directly in code for example with var_dump()
.
例如,如果我們有 user
關系:
For example if we have user
relation:
/**
* @return yiidbActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
然后你可以像這樣var_dump()
原始SQL:
You can then var_dump()
the raw SQL like that:
var_dump($model->getUser()->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
exit();
請注意,您應該這樣調用它,而不是 $model->user->...
(后者返回 User
實例).
Note that you should call it like that and not $model->user->...
(the latter returns User
instance).
但在您的情況下這是不可能的,因為 count()
立即返回 int
.可以var_dump()
部分查詢,不用count()
,但我覺得不方便.
But in your case it's not possible because count()
immediately returns int
. You can var_dump()
partial query without count()
, but I think it's not convenient.
請注意,您可以使用此方法轉儲任何 ActiveQuery
實例(不僅是關系返回的實例)的生成 SQL,例如:
Note that you can use this method for dumping generated SQL of any ActiveQuery
instances (not only those that were returned by relation), for example:
$query = User::find()->where(['status' => User::STATUS_ACTIVE]);
var_dump($query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
exit();
方法二
在我看來這要簡單得多,我個人在調試 SQL 查詢時更喜歡這個.
This is much simpler in my opinion and I personally prefer this one when debugging SQL queries.
Yii 2 有內置的調試模塊.只需將此添加到您的配置中:
Yii 2 has built-in debug module. Just add this to your config:
'modules' => [
'debug' => [
'class' => 'yiidebugModule',
],
],
確保你只在本地擁有它,而不是在生產中.如果需要,還可以更改 allowedIPs
屬性.
Make sure you only have it locally and not on production. If needed, also change allowedIPs
property.
這會在頁面底部為您提供功能面板.找到 DB
詞并單擊計數或時間.在此頁面上,您可以查看所有已執行的查詢并對其進行過濾.我通常不會在 Grid 中過濾它們,而是使用標準瀏覽器搜索來快速導航并找到必要的查詢(例如使用表名作為關鍵字).
This gives you functional panel at the bottom of the page. Find the DB
word and click on either count or time. On this page you can view all executed queries and filter them.
I usually don't filter them in Grid and use standard browser search to quickly navigate through and find the necessary query (using the table name as keyword for example).
方法三
只需在查詢中出錯,例如列名 - cityy
而不是 city
.這將導致數據庫異常,然后您可以立即在錯誤消息中看到生成的查詢.
Just make an error in query, for example in column name - cityy
instead of city
. This will result as database exception and then you can instantly see the generated query in error message.
這篇關于使用 ActiveRecord 和 Yii2 記錄實際的 SQL 查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!