I need to filter records by a field in a belongstomany association (tables are Users and Groups, intermediate table is UsersGroups) but when I put UsersGroups.group_id in the where clause it gives me exception The UsersGroups
association is not defined on Users
.
How can I solve this issue?
This is the code for index action:
public function index()
{
$this->paginate = [
'contain' => ['Profiles', 'Referrer', 'UsersGroups.Groups', 'UsersGroups.Nets'],
];
if ($this->controllerUser['isAdmin'] || $this->controllerUser['isGeneralCoordinator']){
$users = $this->paginate($this->Users->find('all')->where(['Users.profile_id IN' => [1,2,3,4,5,6,7]]));
} else if ($this->controllerUser['isGroupCoordinator']){
$users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id in' => $this->controllerUser['group_id'],
'Users.profile_id IN' => [3,4,5,6]]));
} else if ($this->controllerUser['isNetCoordinator']){
$users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'Users.profile_id IN' => [4,5,6,7]]));
} else if ($this->controllerUser['isNodeCoordinator']){
$users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'UsersGroups.main_gate' => $this->controllerUser['main_gate'],
'Users.profile_id IN' => [4,5,6,7]]));
} else if ($this->controllerUser['isTacticOperator']){
$users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'UsersGroups.main_gate' => $this->controllerUser['main_gate'],
'Users.profile_id IN' => [4,5,6,7]]));
} else if ($this->controllerUser['isSanitaryAgent'] ){
$users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'UsersGroups.main_gate' => $this->controllerUser['main_gate'],
'Users.profile_id IN' => [4,5,6,7]]));
}
$this->set(compact('users'));
}