new: [internal] CRUD component now accepts override fields for the data to be patched

- values derived from for example the currently authed user can be set in the data to be created for example
pull/17/head
iglocska 2020-06-25 01:45:28 +02:00
parent ecc81bdb00
commit df49374103
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 14 additions and 2 deletions

View File

@ -48,11 +48,17 @@ class CRUDComponent extends Component
}
}
public function add(): void
public function add(array $params = []): void
{
$data = $this->Table->newEmptyEntity();
if ($this->request->is('post')) {
$data = $this->Table->patchEntity($data, $this->request->getData());
$input = $this->request->getData();
if (!empty($params['override'])) {
foreach ($params['override'] as $field => $value) {
$input[$field] = $value;
}
}
$data = $this->Table->patchEntity($data, $input);
if ($this->Table->save($data)) {
$message = __('{0} added.', $this->ObjectAlias);
if ($this->Controller->ParamHandler->isRest()) {
@ -81,6 +87,12 @@ class CRUDComponent extends Component
}
$data = $this->Table->get($id, isset($params['get']) ? $params['get'] : []);
if ($this->request->is(['post', 'put'])) {
$input = $this->request->getData();
if (!empty($params['override'])) {
foreach ($params['override'] as $field => $value) {
$input[$field] = $value;
}
}
$this->Table->patchEntity($data, $this->request->getData());
if ($this->Table->save($data)) {
$message = __('{0} updated.', $this->ObjectAlias);