new: [Organisation] table capture function added
parent
7159034e9e
commit
a47dd94011
|
@ -5,11 +5,16 @@ namespace App\Model\Table;
|
|||
use App\Model\Table\AppTable;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\Validation\Validator;
|
||||
use Cake\Error\Debugger;
|
||||
|
||||
class OrganisationsTable extends AppTable
|
||||
{
|
||||
public $metaFields = 'organisation';
|
||||
|
||||
protected $_accessible = [
|
||||
'id' => false
|
||||
];
|
||||
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config);
|
||||
|
@ -47,4 +52,47 @@ class OrganisationsTable extends AppTable
|
|||
->requirePresence(['name', 'uuid'], 'create');
|
||||
return $validator;
|
||||
}
|
||||
|
||||
public function captureOrg($org): ?int
|
||||
{
|
||||
if (!empty($org['id'])) {
|
||||
unset($org['id']);
|
||||
}
|
||||
if (!empty($org['uuid'])) {
|
||||
$existingOrg = $this->find()->where([
|
||||
'uuid' => $org['uuid']
|
||||
])->first();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
if (empty($existingOrg)) {
|
||||
$data = $this->newEmptyEntity();
|
||||
$data = $this->patchEntity($data, $org, ['associated' => []]);
|
||||
if (!$this->save($data)) {
|
||||
return null;
|
||||
}
|
||||
$savedOrg = $data;
|
||||
} else {
|
||||
$reserved = ['id', 'uuid', 'metaFields'];
|
||||
foreach ($org as $field => $value) {
|
||||
if (in_array($field, $reserved)) {
|
||||
continue;
|
||||
}
|
||||
$existingOrg->$field = $value;
|
||||
}
|
||||
if (!$this->save($existingOrg)) {
|
||||
return null;
|
||||
}
|
||||
$savedOrg = $existingOrg;
|
||||
}
|
||||
$this->postCaptureActions($savedOrg->id, $org);
|
||||
return $savedOrg->id;
|
||||
}
|
||||
|
||||
public function postCaptureActions($id, $org)
|
||||
{
|
||||
if (!empty($org['metaFields'])) {
|
||||
$this->saveMetaFields($id, $org);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue