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 processpull/92/head
parent
d488f01051
commit
932a28288d
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue