E
E
Evgeniy Liferov2018-09-26 11:41:48
Yii
Evgeniy Liferov, 2018-09-26 11:41:48

How to form a SQL query in Yii2?

Hello, help me to form a SQL query in Yii2.
There are 3 tables:
This SQL query works and outputs everything correctly, but I can’t figure out how to translate it into Yii2, after looking through the documentation I couldn’t display the second INNER JOIN (I output only one).

SELECT * FROM bd.group 
INNER JOIN bd.account2group ON bd.group.id = bd.account2group.group_id 
INNER JOIN bd.account ON bd.account2group.account_id = bd.account.id
WHERE bd.group.id = 8

Here is an example code I wrote in php and it doesn't work very well :(
$model = Group::find()
    ->innerJoin('account2group', 'id = account2group.group_id')
    ->innerJoin('account', 'account2group.account_id = account.id')
    ->where(['id' => 8])
    ->asArray()
    ->all();
return $model;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2018-09-26
@VladimirAndreev

in the query by group_id filtering, in the code for Yii2 - by id.
it's a bit unsurprising that different filters have different results...
ps and it's better to use ->andWhere )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question