Skip to content

Instantly share code, notes, and snippets.

@lepig
Forked from dcb9/get-yii2-ar-raw-sql.php
Created May 18, 2018 05:36
Show Gist options
  • Select an option

  • Save lepig/0f736a38b9fff9212839a98d0b2127e5 to your computer and use it in GitHub Desktop.

Select an option

Save lepig/0f736a38b9fff9212839a98d0b2127e5 to your computer and use it in GitHub Desktop.
如何获取 YII2 AR 执行的 SQL 语句,直接用程序输出,而不是通过日志去查看
<?php
$query = User::find()
->where(['id'=>[1,2,3,4])
->select(['username'])
// get the AR raw sql in YII2
$commandQuery = clone $query;
echo $commandQuery->createCommand()->getRawSql();
$users = $query->all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment