S
S
sleeplessaek2017-06-27 16:43:25
Yii
sleeplessaek, 2017-06-27 16:43:25

How to exclude records from ActiveRecord whose id is obtained in another ActiveRecord?

There is a table of users Users
There is a table of user groups Groups
There is a table for linking users and groups group_users
There is a getUsers() method in the Groups model

public function getUsers()
    {
        return $this->hasMany(Users::className(), ['id' => 'user_id'])->via('groupUsers');
    }

This method works perfectly and correctly.
There is a Team model, and a table for the team_users connection
And exactly the same method
public function getUsers()
    {
        return $this->hasMany(Users::className(), ['id' => 'user_id'])->via('teamUsers');
    }

You need to get a list of all users from the group not included in the team. I know how to do it via innerJoin and how to do it via array_diff_key().
How to do it through ActiveRecord?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-06-28
@slo_nik

Goodnight. You can also use Join
in ActiveRecord

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question