# How to name a belongstomany association in controller with cakephp 4.4

**URL:** <https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068>\
**Category:** Need Help\
**Created:** [March 19, 2023, 11:51pm UTC](https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068 "2023-03-19T23:51:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [March 19, 2023, 11:51pm UTC](https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068/1 "2023-03-19T23:51:25Z")

</div>

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:

```auto
    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'));
    }
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 20, 2023, 2:00am UTC](https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068/2 "2023-03-20T02:00:09Z")

</div>

`belongsToMany` relations are done in a separate query. What you’re looking for is [`matching`](https://book.cakephp.org/4/en/orm/query-builder.html#filtering-by-associated-data)?

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [March 20, 2023, 3:53pm UTC](https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068/3 "2023-03-20T15:53:08Z")

</div>

Yes, I looked in google and found that, but I also need to filter by another field of the intermediate table UsersGroups, this has this fields: id, user\_id, group\_id, net\_id, main\_gate and the typical created and modified, but I need to filter by group\_id, net\_id and main\_gate, how can I do that?

---

<div class="post-metadata">

**Author:** ![Kaiser85](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kaiser85/32/2714_2.png) [@Kaiser85](https://discourse.cakephp.org/u/Kaiser85)\
**Post date:** [May 5, 2023, 4:23pm UTC](https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068/4 "2023-05-05T16:23:56Z")

</div>

Does the `belongsToMany` association is being made in your `UsersTable.php`?

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 9, 2023, 2:53am UTC](https://discourse.cakephp.org/t/how-to-name-a-belongstomany-association-in-controller-with-cakephp-4-4/11068/5 "2023-05-09T02:53:28Z")

</div>

The problem is that Users is associated to Groups and not directly associated to the joinTable.  
The easy way is to define the association that is not defined. In some cases it can be handy…
