# Why does patchentity overwrite values that are actually not set to null?

**URL:** <https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244>\
**Category:** Need Help\
**Created:** [September 12, 2024, 12:40pm UTC](https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244 "2024-09-12T12:40:14Z")\
**Posts on this page:** 5\
**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:** [September 12, 2024, 12:40pm UTC](https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244/1 "2024-09-12T12:40:14Z")

</div>

Hi, how how can i tell patchentity to not set values to null if they are not set in $requestData.

eg:

````auto
  $accessibleFields = [
                'body' => true,
                'active' => true,
                'authdesc' => true,
                'auth_id' => true,
                'authowner' => true
            ];
            
                                
            $docs = $this->Types->Docs->patchEntity($docs, $requestData, [
                'accessibleFields' => [
                    docsimages' => true
                ],
                'associated' => [
                    Docsimages' => [
                        'accessibleFields' => $accessibleFields
                    ]
                ],
            ]);```  

$requestData looks like that:

````

debug($requestData);  
[  
‘docsimages’ =\> [  
(int) 0 =\> [  
‘id’ =\> ‘453’,  
‘body’ =\> ‘12334’,  
‘active’ =\> (int) 1,  
],  
(int) 1 =\> [  
‘id’ =\> ‘456’,  
‘authdesc’ =\> ‘test’,  
‘auth\_id’ =\> ‘2’,  
‘authowner’ =\> ‘tester’,  
‘body’ =\> ‘656123’,  
‘active’ =\> (int) 1,  
],  
(int) 2 =\> [  
‘id’ =\> ‘455’,  
‘body’ =\> ‘1235656’,  
‘active’ =\> (int) 1,  
],```

as you can see 'authdesc, ‘auth\_id’ , ‘authowner’ are not always set in $requestData but they should be ignore if they are not set instead of being set to null… the old value should be kept in the db…

any idea?

---

<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:** [September 12, 2024, 1:29pm UTC](https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244/2 "2024-09-12T13:29:52Z")

</div>

I have never seen Cake overwrite values in an entity when patching with data that doesn’t contain those fields. If your data really is as shown, `authdesc` should not be touched at all in the entities for IDs 453 and 455. If they are being changed, it’s some other code of yours, maybe something like `beforeMarshal` is filling in fields that are not present?

---

<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:** [September 12, 2024, 2:12pm UTC](https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244/3 "2024-09-12T14:12:27Z")

</div>

well if i say debug($docs);exit; after i do the patchentity it will set ‘authdesc’  
‘authowner’ to null in $docs if there was no provided value for them…

---

<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:** [September 12, 2024, 2:19pm UTC](https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244/4 "2024-09-12T14:19:23Z")

</div>

in additional there is only my validation in the table:

```auto
        $validator
        ->notEmptyString('authdesc', 'Error', function ($context) {
            return isset($context['newRecord']) && $context['newRecord'] === true && isset($context['data']['auth_id']) && in_array($context['data']['auth_id'], [3, 4, 5, 9]);
        })
        ->maxLength('authdesc', 25, 'Error', function ($context) {
            return isset($context['newRecord']) && $context['newRecord'] === true && isset($context['data']['auth_id']) && in_array($context['data']['auth_id'], [3, 4, 5, 9]);
        })
        ->minLength('authdesc', 5, 'Error.', function ($context) {
            return isset($context['newRecord']) && $context['newRecord'] === true && isset($context['data']['auth_id']) && in_array($context['data']['auth_id'], [3, 4, 5, 9]);
        })
        ->add('authdesc', 'validFormat', [
            'rule' => function ($value, $context) {
            return preg_match('/^[a-zA-Z0-9äöüÄÖÜß$%§!"+\-.,?()\/ \r\n]*$/', $value) === 1;
            },
            'message' => 'Error',
            'on' => function ($context) {
            return isset($context['newRecord']) && $context['newRecord'] === true && isset($context['data']['auth_id']) && in_array($context['data']['auth_id'], [3, 4, 5, 9]);
            }
            ])
            
        ->add('authdesc', 'noChange', [
            'rule' => function ($value, $context) {
        return isset($context['newRecord']) && $context['newRecord'] === true;
            },
            'message' => 'Error',
            'on' => function ($context) {
            return isset($context['newRecord']) && $context['newRecord'] === false;
            }
            ]);

```

---

<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:** [September 12, 2024, 10:45pm UTC](https://discourse.cakephp.org/t/why-does-patchentity-overwrite-values-that-are-actually-not-set-to-null/12244/5 "2024-09-12T22:45:31Z")

</div>

Can you show us what `debug($docs)` looks like before and after patching?
