可以用 $model->find()->createCommand()->getRawSql(); 不能带 all(). find() 它的 返回值类型是 yii\db\ActiveQuery, 而find()->all() 返回的就是数组了。
举个栗子
$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();