久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 ActiveRecord 和 Yii2 記錄實際的 SQL 查詢?

Log the actual SQL query using ActiveRecord with Yii2?(使用 ActiveRecord 和 Yii2 記錄實際的 SQL 查詢?)
本文介紹了使用 ActiveRecord 和 Yii2 記錄實際的 SQL 查詢?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在這樣做:

$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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 81精品国产乱码久久久久久 | 国产精品一级在线观看 | 精品国产一区二区三区性色 | 中文字幕在线免费视频 | 欧美一级毛片久久99精品蜜桃 | 永久免费视频 | 久草视频在线播放 | 色婷婷综合成人av | 久久久久久久电影 | 日本 欧美 国产 | 99国产精品99久久久久久 | 欧美激情在线播放 | 五月婷婷色 | 国产精品1区2区 | 国产精品久久久久久久久久久久久 | 国产一区二区精华 | 亚洲精品一二区 | www.久草.com| 亚洲国产一区视频 | 精品一级 | 一区二区三区欧美 | 精品1区 | 成人av色| 欧美亚洲国产日韩 | 精品少妇一区二区三区在线播放 | 伊人网影院 | 国产精品久久国产精品 | 天天看天天操 | 欧美又大粗又爽又黄大片视频 | 亚洲欧美一区二区三区视频 | 久久精品欧美一区二区三区不卡 | 久久国产麻豆 | 91麻豆精品国产91久久久资源速度 | 久久se精品一区精品二区 | 欧美久久国产精品 | 在线播放一区 | 一级爱爱片 | 国产精品亚洲一区二区三区在线 | 一区二区国产在线 | 免费在线成人 | 免费国产黄网站在线观看视频 |