From 0b9b54f14a880cabe95732ad1e597fd7df90bcc2 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Fri, 8 Oct 2021 16:51:10 +0200 Subject: [PATCH] chg:[userSetting] Functions for easier manipulation of user settings --- src/Model/Table/UserSettingsTable.php | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Model/Table/UserSettingsTable.php b/src/Model/Table/UserSettingsTable.php index c4354a2..b4168f9 100644 --- a/src/Model/Table/UserSettingsTable.php +++ b/src/Model/Table/UserSettingsTable.php @@ -26,4 +26,34 @@ class UserSettingsTable extends AppTable ->notEmptyString('user_id', __('Please supply the user id to which this setting belongs to')); return $validator; } -} + + public function getSettingByName($user, $name) + { + return $this->find()->where([ + 'user_id' => $user->id, + 'name' => $name, + ])->first(); + } + + public function createSetting($user, $name, $value) + { + $setting = $this->newEmptyEntity(); + $data = [ + 'name' => $name, + 'value' => $value, + 'user_id' => $user->id, + ]; + $setting = $this->patchEntity($setting, $data); + $savedData = $this->save($setting); + return $savedData; + } + + public function editSetting($user, $name, $value) + { + $setting = $this->getSettingByName($user, $name); + $setting = $this->patchEntity($setting, [ + 'value' => $value + ]); + $savedData = $this->save($setting); + return $savedData; + }