diff --git a/CHANGELOG.md b/CHANGELOG.md index 36dedae..793963d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `glorand/laravel-model-settings` will be documented in this file +## 3.5.1 - 2019-11-29 +### Fix +- Check column settings exists directly on the database table + ## 3.5.0 - 2019-10-14 ### Added - Use cache in case of Table Settings [Ref. task](https://github.com/glorand/laravel-model-settings/issues/25) diff --git a/src/Traits/HasSettingsField.php b/src/Traits/HasSettingsField.php index be23577..d232d9b 100644 --- a/src/Traits/HasSettingsField.php +++ b/src/Traits/HasSettingsField.php @@ -6,6 +6,7 @@ use Glorand\Model\Settings\Exceptions\ModelSettingsException; use Glorand\Model\Settings\Managers\FieldSettingsManager; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Schema; /** * Trait HasSettingsField @@ -54,8 +55,7 @@ public function fixSettingsValue() public function getSettingsValue(): array { $settingsFieldName = $this->getSettingsFieldName(); - $attributes = $this->getAttributes(); - if (!Arr::has($attributes, $settingsFieldName)) { + if (!$this->hasSettingsField()) { throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}"); } @@ -86,6 +86,26 @@ public function setPersistSettings(bool $val = true) $this->persistSettings = $val; } + + /** + * @return mixed + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + private function hasSettingsField() + { + try { + return cache()->remember( + config('model_settings.settings_table_cache_prefix') . '::has_field', + now()->addDays(1), + function () { + return Schema::hasColumn($this->getTable(), $this->getSettingsFieldName()); + } + ); + } catch (\Exception $e) { + throw new ModelSettingsException("Cache: " . $e->getMessage()); + } + } + abstract public function getTable(); abstract public function getAttributes();