glorand/laravel-model-settings

Default numeric array values merged after deletion

Closed this issue · 1 comments

Describe the bug
Default numeric array values are being merged after deletion.

To Reproduce
Steps to reproduce the behavior:

  1. Define a numeric array as default settings
public $defaultSettings = [
    'presets' => [
        'first',
        'second',
    ],
]
  1. Add a setting, and save it.
$presets = $model->settings()->get('presets');
$presets[] = 'third';
$model->settings()->set('presets', $presets);

/**
 * Database JSON result:
 * 
 * "presets":[
 *     "first",
 *     "second",
 *     "third"
 *. ],
 */
  1. Remove "second" from the settings and decode the value:
$model->settings()->delete("presets.1");

/**
 * Database JSON result:
 * 
 * "presets": {
 *     "0":"first",
 *     "2":"third"
 * },
 */

$model->settings()->get('presets');

/**
 * JSON result:
 * 
 * "presets": {
 *     "0":"first",
 *     "1":"second",
 *     "2":"third"
 * },
 */

Expected behavior
Now, the default setting 1 with value second is being merged, but it shouldn't exist anymore.

I don't know if this is even possible, but I wanted to ask here before I change to associative array's ;-)

Isn't it possible that if the setting already exists, the defaults aren't being merged anymore?
Or, is there a workaround?

Thanks!

Desktop (please complete the following information):

  • OS: MacOS
  • Browser: Chrome
  • Version: 88

@edwinvdpol Hello, I think the best solution is to use change to associative array's.