ActiveRecord relation uses too many queries in Yii 2.0

Hello,

I am using Yii since its early days. Since Yii2 came out, I dived into it fully and enjoying it. One issue I feel is there which is important to me.

In Yii 1.1, when we fetched records using active record and ‘with’, the ActiveRecord just used a single big JOIN query and retrieved the records from a single query.

Currently even for eager loading, all the relations are queried separately. Even when we use joinWith, it creates a JOIN query, but again queries for the relations. I think this is a waste in many scenarios, for example, if we want to get a person record, where he has a few related fields like city_id, country_id, category_id, role_id, in Yii 1.1, we would have got the data in a single query by using with(‘city’, ‘country’, ‘role’), but in Yii2, for every person, we’ll have 4 queries.

Will it not be an unnecessary burden. Or do we have some way to accomplish what I want.

regards

try methods innerJoin(), leftJoin().

Thanks Pasman for the reply. Can I have some code or guide please?

What I was asking was, how can I populate the following AR using innerJoin with its related data in a single query in Yii2.


$person = Person::find()->with('city', 'country', 'category')->where('age < 30');