Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting raw sql in Yii2 [duplicate]

Tags:

yii2

I have the query:

$popular = self::find()
    ->from(self::tableName() . ' as t')
    ->with('user');

When I try to print sql:

$popular->createCommand()->rawSql

It gives me:

SELECT * FROM "themes" "t"

So can I get full raw query with "join":

SELECT * FROM "themes" "t" LEFT JOIN "users" u ON u.user_id = t.author_id
like image 612
almost_done Avatar asked Jul 21 '15 08:07

almost_done


1 Answers

It's already answered here.

You can var_dump generated SQL for ActiveQuery instances like this:

var_dump($query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);

However, I recommend to use built-in debug model and panel.

P.S. As for your particular query - with() does not perform JOIN, instead it performs additional query and fills relation attributes with the actual related records. To use JOIN, you need explicitly specify it for example with joinWith().

like image 53
arogachev Avatar answered Oct 25 '22 09:10

arogachev