Many to Many and with()


I have the following database tables and relationships:

users - 1 to many <- user_group -> many to 1 - groups

So in Yii i have 2 models, Users and Groups with a Many to Many relationship. My application requires a user to be able to see all the other users they have a relationship with. So a user may belong to several groups and I want to be able to see all of the users friends from all of their groups.

Is it possible to do something like:


If not I'll bust out the sql!

please read http://www.yiiframew…de/database.arr

Yeah, confused the hell out of me! Maybe I'm trying to run before I can walk but you couldnt give me an example could you?

You may do $user->groups[$i]->users to traverse through the users within the same group of the primary user, but I guess that's not what you want. Probably you should write a plain SQL for such a problem and call findBySql().

Thanks Qiang, looping through sounds like it'll work for me. Cheers!