Hi, how how can i tell patchentity to not set values to null if they are not set in $requestData.
eg:
$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?
Zuluru
September 12, 2024, 1:29pm
2
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?
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…
in additional there is only my validation in the table:
$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;
}
]);
Zuluru
September 12, 2024, 10:45pm
5
Can you show us what debug($docs)
looks like before and after patching?