简体   繁体   English

Yii2 活动记录到原始 sql

[英]Yii2 Active Record to raw sql

I would like to convert the active record query of yii2 to raw SQL equivalent to check if the query is working correctly or not.我想将 yii2 的活动记录查询转换为原始 SQL 等效以检查查询是否正常工作。

It is easy to convert the Eloquent ORM of laravel to raw SQL using toSql() method使用方法很容易将 laravel 的 Eloquent ORM 转换为原始 Z9778840A0100CB30C982876A2 到toSql()

https://laravel.com/api/8.x/Illuminate/Database/Query/Builder.html#method_toSql https://laravel.com/api/8.x/Illuminate/Database/Query/Builder.html#method_toSql

Is there any way to achieve this result in yii2, as I can only find the solutions from raw SQL to active record implementation.有什么方法可以在 yii2 中实现此结果,因为我只能找到从原始 SQL 到活动记录实现的解决方案。

If the question is still not clear will add the example for further explaination.如果问题仍然不清楚,将添加示例以进一步解释。

Yes, you can use either the property sql or the method rawSql() of the query->command , for example:是的,您可以使用属性sqlquery->commandrawSql()方法,例如:

$querySql = Model::find()->where(...)->createCommand()->sql;
$querySql = Model::find()->where(...)->createCommand()->getRawSql();

// Or, if you want to use the query somewhere else
$query = Model::find()->where(...);
...
$sql = $query->createCommand()->sql;

If you are just debugging, it is probably easier to see the query on the debug toolbar.如果您只是调试,在调试工具栏上查看查询可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM