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 table
association queryuser data table
, it is still N 1 queries.
How to associate the user table
and user data table
through with in one go?
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.