From a47dd94011a700f5f50b5c1f8f6d062ea6fa5ae0 Mon Sep 17 00:00:00 2001 From: iglocska Date: Wed, 13 Jan 2021 14:19:45 +0100 Subject: [PATCH] new: [Organisation] table capture function added --- src/Model/Table/OrganisationsTable.php | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/Model/Table/OrganisationsTable.php b/src/Model/Table/OrganisationsTable.php index 52f08ac..5eeb756 100644 --- a/src/Model/Table/OrganisationsTable.php +++ b/src/Model/Table/OrganisationsTable.php @@ -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); + } + } }