Wednesday, 11 November 2015

Yii2: Get raw SQL Query from an Activerecord or a query builder

This tutorial show you how to dump the Sql Query from an Activerecord and more.
It can help us to debug Sql query in some case when yii debug is not work.

Example:
$listBooks = Books::find()->where('author=2')->all();

To get raw SQL query with all parameters included try:
$query = Books::find()->where('author=2');
echo $query->createCommand()->sql;
echo $query->createCommand()->getRawSql();

It not only works with Activerecord .
Refer : http://chris-backhouse.com/Yii2-Output-the-SQL-from-a-query-builder/1027