Active Record query

Hi,
I am new in yii2 I am trying write query with join to get all column from two tables
but not understand what the problem
modelName::find() joinWith(‘secondTable’,)
->where([‘firstTable.id’=>$id])
->all();
but when print query show as SELECT firstTable.* FROM firstTable LEFT JOIN secondTable ON firstTable.id = secondTable.id WHERE firstTable.id=‘5’
when trying remove firstTable. from begin of query its done but
I want know why firstTable. added to query

Thanks for help

Hi @newYii2, welcome to the forum.

Yii2’s ActiveRecord uses 2 queries to retrieve the records when you specify a related table. The SQL you showed is the 1st one that retrieves the main table. And you could find the 2nd one which retrieves the records in secondTable.

1 Like

It’s enough for you to use with when you don’t want to filter the main table by the related 2nd table.

FirstModel::find()->with('secondModel')->where(['id' => $id])->one();

This will fetch 1 first model with its related second model(s).

Please check the following wiki for more tips regarding the relational active record.
https://www.yiiframework.com/wiki/780/drills-search-by-a-hasmany-relation-in-yii-2-0