J
J
Julia Kovalenko2016-09-26 10:27:26
MySQL
Julia Kovalenko, 2016-09-26 10:27:26

How to do leftJoin() with two tables in yii2?

Here is a working query in sql:

SELECT * FROM `client` 
LEFT JOIN `user` ON `client`.`client_id` = `user`.`id`
LEFT JOIN `company` ON `user`.`company_id` = `company`.`id`

How can I implement it in yii2? I need dataprovider.
Tried like this but it doesn't work
$dataProvider = new ActiveDataProvider([
    'query' => Client::find()
                ->leftJoin('user', 'client.client_id = user.id')
                ->leftJoin('company', 'user.company_id = company.id')
]);

Here is the db structure
58bb0eec25434bb08f28d8947535a026.jpg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
matperez, 2016-09-26
@kovalenko_jul_s

Write the relations in the User model and you can write $query = User::find()-> joinWith ('client')->joinWith('company').

D
Dmitry Yakovlev, 2016-09-26
@Pr0per

For such simple things, I think it will be enough (item "Working with connected data").

H
hackerdad, 2021-08-19
@hackerdad

'query' => Client::find()->joinWith(['user.company']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question