Reversed relation

Hi there,

I have some newsletters and other types of lists, which my users can subscribe to. I’ve made a class called “UserList”, which has a relation to the users in it, through a table called “userListUsers”.

Some of these userlists are default, which means that nearly all users are subscribed to them. I then thought it would be nice to have a reversed relation, where $userList->users returned all users NOT having a relation to the userlist.

Is it possible in Yii, to reverse a relation, so you get all models NOT related to the current?

Thanks in advance :slight_smile:

You can achive by implementing a get method in the model, like:

public function getNotRelated()


    return Users::model()->findAll("userID not in (SELECT userId FROM userListUser WHERE userListId='.$this->id.')");


I could, but then I wouldn’t be able to use all built in functions as the “with(‘users’)” function and similar.