public function saveSettings() { $settings = []; foreach (Setting::findAll($this->attributes()) as $setting) { $settings[$setting->name] = $setting; } foreach ($this->attributes() as $attribute) { if (!empty($settings[$attribute])) { if ($settings[$attribute]->value != $this->{$attribute}) { $settings[$attribute]->value = $this->{$attribute}; $settings[$attribute]->save(); } } else { $setting = new Setting(); $setting->name = $attribute; $setting->value = $this->{$attribute}; $setting->save(); } } // GC foreach (Setting::find()->where(['not in', 'name', $this->attributes()])->all() as $setting) { $setting->delete(); } }