登录使用的ActiveRecord与Yii2实际的SQL查询? [英] Log the actual SQL query using ActiveRecord with Yii2?

查看:381
本文介绍了登录使用的ActiveRecord与Yii2实际的SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做这样的:

  $学生=学生::找到() - >所有的();
    返回$这个 - >渲染('过程',阵列('学生'=> $学生));
 

,然后这个视图:

 的foreach($学生为$学生)
    {
        回声$学生为>的名字。 ,和放大器; NBSP;';
        回声$学生为> getQuizActivitiesCount(); ?> < BR /> < PHP
    }
 

我想看看正在执行的SQL查询。一个学生有很多竞猜活动,以及查询执行完美,但我需要看到原始的SQL。这可能吗?

解决方案

方法1

通过关系返回警予\ DB \ ActiveQuery 实例,它可以直接提取出原始的SQL查询,例如code与的var_dump ()

例如,如果我们有用户关系:

  / **
 * @返回\警予\ DB \ ActiveQuery
 * /
公共职能的getUser()
{
    返回$这个 - > hasOne(用户::类名(),['身份证'=>'user_id说明']);
}
 

您可以再的var_dump()的原始SQL这样的:

<$p$p><$c$c>var_dump($model->getUser()->$p$ppare(Yii::$app->db->queryBuilder)->createCommand()->rawSql); 出口();

请注意,你应该称呼它的,而不是 $建模&GT;用户自&GT; ... (后者返回用户实例)。

不过你的情况是不可能的,因为计数()立即返回 INT 。您可以的var_dump()没有部分查询计数(),但我认为这不是方便。

请注意,您可以使用此方法倾倒任何的ActiveRecord 实例生成的SQL,例如:

  $查询=用户::找到() - 化合物其中(['状态'=&gt;用户:: STATUS_ACTIVE]);
后续代码var_dump($查询);
出口();
 

方法2

这是在我看来更简单,我亲自调试SQL查询时,preFER这一个。

Yii的2还内置调试模块。只需添加到您的配置:

 '模块'=&GT; [
    调试=&GT; [
        类=&GT; 警予\调试\模块',
    ]
]
 

请确保你只有在本地,而不是生产。如果需要的话,也改变 allowedIPs 属性。

这让你在页面底部的功能面板。找到 DB 文字,然后点击任一数或时间。在此页面可以查看所有执行的查询和过滤。 我通常不过滤它们在网格和使用标准的浏览器搜索,快速浏览和查找所需的查询(使用表名作为关键字,例如)。

I'm doing this:

$students = Student::find()->all();
    return $this->render('process', array('students' => $students));

and then this in the view:

foreach($students as $student)
    {
        echo $student->name . ', &nbsp;';
        echo $student->getQuizActivitiesCount(); ?> <br /> <?php
    }

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?

解决方案

Method 1

With relations that return yii\db\ActiveQuery instance it's possible to extract the raw SQL query directly in code for example with var_dump().

For example if we have user relation:

/**
 * @return \yii\db\ActiveQuery
 */
public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'user_id']);
}

You can then var_dump() the raw SQL like that:

var_dump($model->getUser()->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
exit();

Note that you should call it like that and not $model->user->... (the latter returns User instance).

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.

Note that you can use this method for dumping generated SQL of any ActiveRecord instances, for example:

$query = User::find()->where(['status' => User::STATUS_ACTIVE]);
var_dump($query);
exit();

Method 2

This is much simpler in my opinion and I personally prefer this one when debugging SQL queries.

Yii 2 has built-in debug module. Just add this to your config:

'modules' => [
    'debug' => [
        'class' => 'yii\debug\Module',
    ],
],

Make sure you only have it locally and not on production. If needed, also change allowedIPs property.

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).

这篇关于登录使用的ActiveRecord与Yii2实际的SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆