# Wrong user\_id after saving why?

**URL:** <https://discourse.cakephp.org/t/wrong-user-id-after-saving-why/9782>\
**Category:** Need Help\
**Created:** [October 6, 2021, 10:12pm UTC](https://discourse.cakephp.org/t/wrong-user-id-after-saving-why/9782 "2021-10-06T22:12:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [October 6, 2021, 10:12pm UTC](https://discourse.cakephp.org/t/wrong-user-id-after-saving-why/9782/1 "2021-10-06T22:12:40Z")

</div>

Hello, i wrote a function to add a friend to a user but if i save the $data it will use a user\_id that differs from what i provided.

debug($data) shows:

**APP/Controller/UsersController.php** (line **243** )  
[‘friends’ =\> [(int) 0 =\> [‘friendslist\_id’ =\> (int) 7,‘user\_id’ =\> (int) 1,‘approved’ =\> (int) 33333,],],

but the database entry shows: user\_id’ =\> (int) 7

Function is:

$user = $this-\>Users-\>get($id, [  
‘contain’ =\> [‘Friendslists.Friends’],  
]);

$data = [  
‘friends’ =\> [  
[  
‘friendslist\_id’ =\>$user-\>friendslist-\>id,  
‘user\_id’ =\> $this-\>request-\>getAttribute(‘identity’)-\>getIdentifier(),  
‘approved’ =\> 33333,  
],],

];

//debug($data);exit;  
$user-\>setDirty(‘friends’, true);  
$user = $this-\>Users-\>patchEntity($user, $data);  
$this-\>Users-\>save($user);

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [October 7, 2021, 2:51am UTC](https://discourse.cakephp.org/t/wrong-user-id-after-saving-why/9782/2 "2021-10-07T02:51:37Z")

</div>

Maybe you have a error in your asociation?

---

<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:** [October 7, 2021, 5:59am UTC](https://discourse.cakephp.org/t/wrong-user-id-after-saving-why/9782/3 "2021-10-07T05:59:48Z")

</div>

What does `debug($user->friends)` give you after the `patchEntity` call?
