php - Yii relational table with optimized query to avoid N+1 query questions
阿神
阿神 2017-06-10 09:47:45
0
2
1211

Example: A certain post details page
Details page hasMany associationComment table
Comment table hasOne associationUser table
User table hasOne associationUser data table

Then for the comment list, you can query the user table through with('author') association, avoiding N 1 queries

But at this time, through user tableassociation queryuser data table, it is still N 1 queries.

How to associate the user table and user data table through with in one go?

阿神
阿神

闭关修行中......

reply all(2)
学霸

Comment associated user: getAuthor
User associated user data: getAuthorname
Can be retrieved with ('author.authorname').
If you want to see an example, this video chapter just talks about retrieving data from multi-layer associations.

世界只因有你

I haven’t used yii2 for a long time. When I used ActiveRecord, I would directly write SQL to handle complex statements. I was too lazy to change the model file here and there. Use native SQL, as long as you think the performance is fine. , LEFT JOIN association is enough, use findBySQL() to check.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!