# patchEntity does not patch data properly with association

**URL:** <https://discourse.cakephp.org/t/patchentity-does-not-patch-data-properly-with-association/7470>\
**Category:** Need Help\
**Created:** [March 14, 2020, 7:17pm UTC](https://discourse.cakephp.org/t/patchentity-does-not-patch-data-properly-with-association/7470 "2020-03-14T19:17:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matibaski](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/matibaski/32/1717_2.png) [@matibaski](https://discourse.cakephp.org/u/matibaski)\
**Post date:** [March 14, 2020, 7:17pm UTC](https://discourse.cakephp.org/t/patchentity-does-not-patch-data-properly-with-association/7470/1 "2020-03-14T19:17:38Z")

</div>

I have following table I am using for a messages function:

```
CREATE TABLE `messages` (
  `id` int(11) NOT NULL PRIMARY AUTO_INCREMENT,
  `to_user` int(11) NOT NULL,
  `from_user` int(11) NOT NULL,
  `message` text,
  `seen` tinyint(1) NOT NULL DEFAULT '0',
  `created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

```

I want now to create a new message the standard cakephp-way:

```
$message = $this->Messages->newEmptyEntity();
$message = $this->Messages->patchEntity($message, $this->request->getData());
$this->Messages->save($message);

```

This is my data that needs to be saved:

```
[
	'to_user' => (int) 2,
	'message' => 'test',
	'from_user' => (int) 1,
	'seen' => false
]

```

But it only saves the fields `message` and `seen`, even if all fields are accessible. I debugged the patched entity and got following result:

```
object(App\Model\Entity\Message) {

	'message' => 'test',
	'seen' => false,
	'[new]' => true,
	'[accessible]' => [
		'to_user' => true,
		'from_user' => true,
		'message' => true,
		'seen' => true,
		'created' => true
	],
	'[dirty]' => [
		'message' => true,
		'seen' => true
	],
	'[original]' => [],
	'[virtual]' => [],
	'[hasErrors]' => false,
	'[errors]' => [],
	'[invalid]' => [],
	'[repository]' => 'Messages'

}

```

When I remove the `belongsTo` relation, it works:

```
$this->belongsTo('ToUser', [
    'foreignKey' => 'to_user',
    'className' => 'Users',
    'propertyName' => 'to_user'
]);
$this->belongsTo('FromUser', [
    'foreignKey' => 'from_user',
    'className' => 'Users',
    'propertyName' => 'from_user'
]);

```

Now I don’t know how to debug or fix this issue. Any ideas?

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [March 14, 2020, 8:09pm UTC](https://discourse.cakephp.org/t/patchentity-does-not-patch-data-properly-with-association/7470/2 "2020-03-14T20:09:15Z")

</div>

you have field the same name as association and cake doesnt understand which one you want to use, thats why there are [https://book.cakephp.org/3/en/intro/conventions.html#database-conventions](https://book.cakephp.org/3/en/intro/conventions.html#database-conventions) for naming

---

<div class="post-metadata">

**Author:** ![matibaski](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/matibaski/32/1717_2.png) [@matibaski](https://discourse.cakephp.org/u/matibaski)\
**Post date:** [March 15, 2020, 12:55pm UTC](https://discourse.cakephp.org/t/patchentity-does-not-patch-data-properly-with-association/7470/3 "2020-03-15T12:55:52Z")

</div>

Thank you.

I changed the sql-table fields to `from_user_id` and `to_user_id`, edited the Model and the relations were fixed and saving new entries were possible.

Here is my new `MessagesTable`:

```
    $this->belongsTo('ToUsers', [
        'foreignKey' => 'to_user_id',
        'joinType' => 'INNER',
        'className' => 'Users',
        'propertyName' => 'from_user'
    ]);
    $this->belongsTo('FromUsers', [
        'foreignKey' => 'from_user_id',
        'joinType' => 'INNER',
        'className' => 'Users',
        'propertyName' => 'from_user'
    ]);
```
