6

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
almost_done
  • 397
  • 2
  • 4
  • 15

2 Answers2

13

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().

Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117
-2

Hope this will help

$query = new Query;

$query  ->select(['themes.c1 AS d1', 'themes.c2 As d2..'])

        ->from('themes')

        ->leftJoin('users', 'users.user_id = themes.author_id');

$command = $query->createCommand();

$data = $command->queryAll();
ekad
  • 14,436
  • 26
  • 44
  • 46
sujoi
  • 95
  • 1
  • 1