为了账号安全,请及时绑定邮箱和手机立即绑定

yii2.0获取到最后一条执行的sql怎么看

yii2.0获取到最后一条执行的sql怎么看

Yii
POPMUISE 2019-02-12 09:25:42
yii2.0获取到最后一条执行的sql怎么看
查看完整描述

1 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

这个有很多种方法

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



 

 


查看完整回答
反对 回复 2019-02-16
  • 1 回答
  • 0 关注
  • 591 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信