Skip to content

Commit

Permalink
Check column settings exists directly on the database table (#32)
Browse files Browse the repository at this point in the history
Check column settings exists directly on the database table
  • Loading branch information
glorand authored Nov 29, 2019
1 parent 79a2f0a commit 213c2c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions src/Traits/HasSettingsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}");
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 213c2c4

Please sign in to comment.