diff --git a/app/GirlsInDigitalPage.php b/app/GirlsInDigitalPage.php index 2b837d1fe..5dfabff37 100644 --- a/app/GirlsInDigitalPage.php +++ b/app/GirlsInDigitalPage.php @@ -63,43 +63,44 @@ class GirlsInDigitalPage extends Model 'locale_overrides' => 'array', ]; + /** + * Button updates from Nova form; never persisted to DB. + * + * @var array>|null + */ + public $nonPersistedButtonUpdates; + /** * Prevent relationship / virtual attributes from being persisted (e.g. from Nova form). + * Store _button_updates only in nonPersistedButtonUpdates so it never hits the DB. */ public function setAttribute($key, $value) { if (in_array($key, ['faqItems'], true)) { return $this; } + if ($key === '_button_updates') { + $this->nonPersistedButtonUpdates = $value; + + return $this; + } if (str_starts_with($key, 'button_') || str_starts_with($key, 'locale_')) { return $this; } + return parent::setAttribute($key, $value); } /** - * Used by Nova afterSave; not persisted to DB. - * - * @var array>|null - */ - public $nonPersistedButtonUpdates; - - /** - * Keep _button_updates out of the SQL UPDATE (used by Nova to pass button data to afterSave). + * So that $model->_button_updates reads back the non-persisted value. */ - protected static function booted(): void + public function getAttribute($key) { - static::saving(function (self $model): void { - $model->preserveButtonUpdatesBeforeSave(); - }); - } - - private function preserveButtonUpdatesBeforeSave(): void - { - if (array_key_exists('_button_updates', $this->attributes)) { - $this->nonPersistedButtonUpdates = $this->attributes['_button_updates']; - unset($this->attributes['_button_updates']); + if ($key === '_button_updates') { + return $this->nonPersistedButtonUpdates; } + + return parent::getAttribute($key); } public function buttons() diff --git a/app/Nova/GirlsInDigitalPage.php b/app/Nova/GirlsInDigitalPage.php index 66223c511..cf02e2a46 100644 --- a/app/Nova/GirlsInDigitalPage.php +++ b/app/Nova/GirlsInDigitalPage.php @@ -118,13 +118,22 @@ private function localeField(string $label, string $locale, string $key, bool $t return $overrides[$locale][$key] ?? ''; }); $field->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locale, $key) { - $value = $request->get($requestAttribute); - $overrides = $model->locale_overrides ?? []; - if (! isset($overrides[$locale])) { - $overrides[$locale] = []; + try { + $value = $request->get($requestAttribute); + $overrides = $model->locale_overrides ?? []; + if (! isset($overrides[$locale])) { + $overrides[$locale] = []; + } + $overrides[$locale][$key] = $value; + $model->locale_overrides = $overrides; + } catch (\Throwable $e) { + \Illuminate\Support\Facades\Log::error('GirlsInDigital locale field fill failed: ' . $attribute, [ + 'exception' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + ]); + throw $e; } - $overrides[$locale][$key] = $value; - $model->locale_overrides = $overrides; }); return $field; } @@ -335,16 +344,25 @@ private function getButtonField(string $key, string $field) private function fillButtonField(\App\GirlsInDigitalPage $model, string $key, string $field, $value): void { - if (! isset($model->_button_updates)) { - $model->_button_updates = []; - } - if (! isset($model->_button_updates[$key])) { - $model->_button_updates[$key] = []; - } - if ($field === 'enabled' || $field === 'open_new_tab') { - $value = (bool) $value; + try { + if (! isset($model->_button_updates)) { + $model->_button_updates = []; + } + if (! isset($model->_button_updates[$key])) { + $model->_button_updates[$key] = []; + } + if ($field === 'enabled' || $field === 'open_new_tab') { + $value = (bool) $value; + } + $model->_button_updates[$key][$field] = $value; + } catch (\Throwable $e) { + \Illuminate\Support\Facades\Log::error('GirlsInDigital button field fill failed: ' . $key . '.' . $field, [ + 'exception' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + ]); + throw $e; } - $model->_button_updates[$key][$field] = $value; } public static function afterSave(NovaRequest $request, $model): void @@ -356,7 +374,7 @@ public static function afterSave(NovaRequest $request, $model): void if (! Schema::hasTable('girls_in_digital_buttons')) { return; } - $updates = $model->nonPersistedButtonUpdates ?? $model->_button_updates ?? null; + $updates = $model->nonPersistedButtonUpdates ?? null; if (! is_array($updates) || empty($updates)) { return; } @@ -380,7 +398,7 @@ public static function afterSave(NovaRequest $request, $model): void report($e); } } - unset($model->_button_updates, $model->nonPersistedButtonUpdates); + unset($model->nonPersistedButtonUpdates); } catch (\Throwable $e) { report($e); }