new: [CRUD] added some new useful features

- afterFind for the edit functions to make last minute decisions on the modification after already having loaded the data to be modified
- moved the field restrictions to be able to pass it to the view
- try/catch for bulk deletions. A single failure in the beforeSave call will no longer block the entire saving process
pull/92/head
iglocska 2022-01-21 13:41:29 +01:00
parent d488f01051
commit 932a28288d
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 14 additions and 10 deletions

View File

@ -157,9 +157,6 @@ class CRUDComponent extends Component
{
$this->getMetaTemplates();
$data = $this->Table->newEmptyEntity();
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
if ($this->request->is('post')) {
$patchEntityParams = [
'associated' => [],
@ -223,6 +220,9 @@ class CRUDComponent extends Component
}
}
}
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
$this->Controller->entity = $data;
$this->Controller->set('entity', $data);
}
@ -295,21 +295,18 @@ class CRUDComponent extends Component
$data->where($params['conditions']);
}
$data = $data->first();
if (isset($params['afterFind'])) {
$data = $params['afterFind']($data, $params);
}
if (empty($data)) {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
}
$data = $this->getMetaFields($id, $data);
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
if ($this->request->is(['post', 'put'])) {
$patchEntityParams = [
'associated' => []
];
$input = $this->__massageInput($params);
if (!empty($params['fields'])) {
$patchEntityParams['fields'] = $params['fields'];
}
$data = $this->Table->patchEntity($data, $input, $patchEntityParams);
if (isset($params['beforeSave'])) {
$data = $params['beforeSave']($data);
@ -352,6 +349,9 @@ class CRUDComponent extends Component
}
}
}
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
$this->Controller->entity = $data;
$this->Controller->set('entity', $data);
}
@ -469,7 +469,11 @@ class CRUDComponent extends Component
}
$data = $data->first();
if (isset($params['beforeSave'])) {
$data = $params['beforeSave']($data);
try {
$data = $params['beforeSave']($data);
} catch (Exception $e) {
$data = false;
}
}
if (!empty($data)) {
$success = $this->Table->delete($data);