简体   繁体   中英

Yii find all relation of a model

I'm dealing with Yii framework and I have a little question about find. This is my situation:

I have a model user and a relation user->friends. So, to get all the friends, I just need to do $userModel->friends. And this will return an array containing all the user model that are his friends. Now, what I want to do, is, given a list of user, loop over it, for each user check if this user is a friend. I know I can user User::model()->exists, but how? I'm a bit lost here.

Thank you

Why not to put your relations() code here? If friends relation itself has defined options by which another User is a friend, you do not need to check this again. Just traverse friends

foreach ( $userModel->friends as $friend ) {
    var_dump( $friend );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM