# Associations do not work at all

**URL:** <https://discourse.cakephp.org/t/associations-do-not-work-at-all/3501>\
**Category:** Need Help\
**Created:** [November 15, 2017, 10:04pm UTC](https://discourse.cakephp.org/t/associations-do-not-work-at-all/3501 "2017-11-15T22:04:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mantoshelis](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Mantoshelis](https://discourse.cakephp.org/u/Mantoshelis)\
**Post date:** [November 15, 2017, 10:04pm UTC](https://discourse.cakephp.org/t/associations-do-not-work-at-all/3501/1 "2017-11-15T22:04:08Z")

</div>

Hello,

I have two tables:

![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/1X/f623b67d80c9aff7a84e26095873cc81c6f1350d.png)

I want to make association in CakePHP v3.x like this:

**UsersTable.php** :

```
<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->hasOne('UserRoles');
    }
}

```

**UserRolesTable.php** :

```
<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class UserRolesTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Users');
    }
}

```

What query did **Cake\ORM\Query** object contain. As you see no **JOIN**!:

```
SELECT Users.user_id AS `Users__user_id`,
       Users.username AS `Users__username`,
       Users.password AS `Users__password`,
       Users.user_role_id AS `Users__user_role_id`,
       Users.internet_plan_id AS `Users__internet_plan_id`
FROM users Users

```

And the result I get after fetch:

```
{
   "user_id":1,
   "username":"admin",
   "password":"abc123",
   "user_role_id":1,
   "internet_plan_id":null
}

```

What i want to get is:

```
{
   "user_id":1,
   "username":"admin",
   "password":"abc123",
   "user_role":{
      "user_role_id":1,
      "name":"ADMINISTRATOR"
   },
   "internet_plan_id":null
}

```

I’m stuck with this issue for at least 2 hours. I tried a lot of combinations but nothing helped. Could someone help me?  
Thank you in advice.

---

<div class="post-metadata">

**Author:** ![arvindh\_s](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@arvindh\_s](https://discourse.cakephp.org/u/arvindh_s)\
**Post date:** [November 16, 2017, 12:19am UTC](https://discourse.cakephp.org/t/associations-do-not-work-at-all/3501/2 "2017-11-16T00:19:02Z")

</div>

Can you post your fetching code? Perhaps you would have missed adding `->contain('Users')` to find query

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 16, 2017, 5:27am UTC](https://discourse.cakephp.org/t/associations-do-not-work-at-all/3501/3 "2017-11-16T05:27:32Z")

</div>

@arvindh_s is right, and as you do not follow conventions you should define foreign key for the associations.  
[https://book.cakephp.org/3.0/en/orm/associations.html#hasone-associations](https://book.cakephp.org/3.0/en/orm/associations.html#hasone-associations)

---

<div class="post-metadata">

**Author:** ![Mantoshelis](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Mantoshelis](https://discourse.cakephp.org/u/Mantoshelis)\
**Post date:** [November 20, 2017, 12:00pm UTC](https://discourse.cakephp.org/t/associations-do-not-work-at-all/3501/4 "2017-11-20T12:00:19Z")

</div>

Thank you, it worked!
