Skip to content

Commit

Permalink
Fixes to reduce excessive number of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bonroyage committed Nov 3, 2023
1 parent c97ef8d commit d5fe67c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/Models/AppSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ class AppSetting extends Model

private bool $didRegisterSettingsBlocks = false;

public static function booted()
{
self::saving(function (self $model) {
/*
* Here we remove the 'blocks' relation so that any developer hooking
* into the saved event on the AppSetting can still fetch the settings
* with the new values. The next time the setting facade is called to
* retrieve a setting, the blocks relation is hydrated again.
*/
$model->unsetRelation('blocks');
});
}

/**
* @return array|array<int,string>
*/
Expand Down
11 changes: 5 additions & 6 deletions src/Services/Settings/SettingsGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SettingsGroup

private ?Closure $availableWhen = null;

private AppSetting $appSetting;

public static function make(): self
{
return new self();
Expand Down Expand Up @@ -70,12 +72,9 @@ public function hasSection(string $sectionName): bool

public function getSettingsModel(): AppSetting
{
$settingsModel = AppSetting::where(['name' => $this->getName()])->first();
if (!$settingsModel) {
$settingsModel = AppSetting::create(['name' => $this->getName()]);
}

return $settingsModel;
return $this->appSetting ??= AppSetting::firstOrCreate([
'name' => $this->getName(),
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/TwillAppSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getGroupDataForSectionAndName(string $group, string $section): B
{
$groupObject = $this->getGroupForGroupAndSectionName($group, $section);

return $groupObject->getSettingsModel()->blocks()
return $groupObject->getSettingsModel()->blocks
->where('editor_name', $section)
->where('parent_id', null)
->firstOrFail();
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/Settings/SettingsFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use A17\Twill\Exceptions\Settings\SettingsGroupDoesNotExistException;
use A17\Twill\Exceptions\Settings\SettingsSectionDoesNotExistException;
use A17\Twill\Facades\TwillAppSettings;
use A17\Twill\Models\AppSetting;
use A17\Twill\Services\Settings\SettingsGroup;
use A17\Twill\Tests\Integration\TestCase;

Expand Down Expand Up @@ -66,6 +67,9 @@ public function setUp(): void
]
)
->assertStatus(200);

/** @see AppSetting::booted() */
$model->unsetRelation('blocks');
}

public function testTranslatedSettingsGetter(): void
Expand Down

0 comments on commit d5fe67c

Please sign in to comment.