V
V
Vladislav2021-02-28 10:00:52
Yii
Vladislav, 2021-02-28 10:00:52

Why is Join not returning the contents of the second table?

Who knows why when join does not matter which inner or left it returns the result only from the first table with which the chain began:

$result = TableOne::find()
            ->select('table_one.user_id, table_two.*')
            ->innerJoin('table_two', 'table_one.post_id = table_two.post_id')
            ->where(['user_id' => Yii::$app->user->identity->id])->all();
        dd($result );

If you do
createCommand()->getRawSql();
AND execute this query directly in phpMyAdmin, the result will be both tables. And Yii only returns fields from TableOne.

And he doesn't care what I pointed out
select('table_one.user_id, table_two.*')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Soshnikov, 2021-02-28
@cr1gger

You are working with ActiveRecord. When selecting from the database, Yii gets everything that you specified in the select.
However, further, when processing data from the database and forming models from them, Yii collects in the resulting array only the list of models for which the request was made.
To use data from the second table, set up model links and use with().
The data from the second table will be available via the $model->myRelation relationship

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question