0

问题描述

Yii2开发中,使用关联查询with后,怎么打印执行执行sql呢?

尝试过

    echo  $lesson->createCommand()->getRawSql();die;

相关代码

$model = CrmLesson::find()
        ->where(['id' => $lessonId])
        ->with([
            'class' => function (ActiveQuery $query){
                $query->select('id, num_code');
            }
        ])
        ->select(['id as lesson_id', 'classroom_id', 'type', 'class_id', 'title', 'teacher', 'teacher_assistant', 'open_time as start_time', 'end_time'])
        ->asArray();
echo  $lesson->createCommand()->getRawSql();die;

实际看到结果


SELECT `id` AS `lesson_id`, `classroom_id`, `type`, `class_id`, `title`, `teacher`, `teacher_assistant`, `open_time` AS `start_time`, `end_time` FROM `crm_lesson` WHERE `id`='8161'
2019-03-13 提问
2 个回答
0

with是分成两句执行的

  1. 执行主查询
  2. 得到关联键数组
  3. 通过关联键再次查询

要看SQL的话,去把log的级别调整一下

0

网页运行的话,要看执行的SQL日志可以在yii-debug工具看到,工具界面:

图片描述

yii2-debug:https://github.com/yiisoft/yi...

撰写答案

推广链接