Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NULL setting value to DatabaseSettingStore #171

Open
quallrum opened this issue Dec 13, 2022 · 1 comment
Open

Add support for NULL setting value to DatabaseSettingStore #171

quallrum opened this issue Dec 13, 2022 · 1 comment

Comments

@quallrum
Copy link

Hello,

Recently i've encountered a problem with storing null as a setting value: a new non-null value isnt stored into database if such a key already exists and have a null value.

// This will create a new setting with nullable value
settings()->set([
    'key' => null,
]);
settings()->save();

// That new value will not be stored into database, the actual value will remain "null"
settings()->set([
    'key' => 'value',
]);
settings()->save();

The cause of this problem is that DatabaseSettingStore uses the isset() function to determine if a specific key exists, but that function does not consider null values. I would suggest to use the array_key_exists() function instead:

class DatabaseSettingStore extends SettingStore
{
    protected function write(array $data): void {
        // ...

        foreach ($keys as $key) {
            if (
                array_key_exists($key, $updatedData)
                && array_key_exists($key, $persistedData)
                && (string) $updatedData[$key] !== (string) $persistedData[$key]
            ) {
                $updateData[$key] = $updatedData[$key];
            } elseif (!array_key_exists($key, $insertData)) {
                $deleteKeys[] = $key;
            }

            unset($insertData[$key]);
        }

        // ...
    }
}

A use-case for this improvement is that sometimes (my case :)) you have a single form for the project settings and some of them may not have a value (null by default) and it is handy to just save all validated settings into database, event if some values are null:

$settings = $request->validated();

settings()->set($settings);
settings()->save();
@pandeydip
Copy link

I am also facing the same issue, most needed improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants