Get all users from all teams of this department

Hello, I’m looking for a way to get all the coworkers in a department of the currently logged in user.

Howver, the user is part of a team (foreign key: team_id, which links to the teams table) and each team is part of a department (foreign key: department_id, which links to the departments table). So far I have this script, but this only gives me the employees from the current users team:

$users = $this->Requests->Users->find('list', [
   'conditions' => ['Users.team_id =' => $this->Auth->user('team_id')]

I’ve done something like this before through some very ugly foreaching and I was wondering what would be the best practise of doing this. I’m hoping somebody can help me out.

There are multiple ways to achieve this and I guess it depends on the situation and your preferences which one you should go with.

What you can’t do is search for users directly - unless you add a department_id field to the users table, which is certainly also an option.

But you can search for teams or departments and then contain users. This would probably be the most cake-typical method.

Another way is to do two queries: First, you get a list of team id’s from that particular department and then search for the users that are within one of those teams.