Files
crewli/api/app/Observers/FormBuilder/FormFieldChildTablesCascadeObserver.php

50 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Observers\FormBuilder;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldLibrary;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Cascades physical deletion of child rows in the three relational tables
* that hang off `form_fields` / `form_field_library`:
*
* - `form_field_bindings` (WS-5a)
* - `form_field_validation_rules` (WS-5b)
* - `form_field_configs` (WS-5b expansion; added in commit 5)
*
* Children represent current state (not historical intent) — physically
* deleted even when the owner is only soft-deleted, per addendum Q3: no
* child table in this family carries a soft-delete semantic of its own.
*
* Renamed from `FormFieldBindingsCascadeObserver` during WS-5b commit 1.
* The `Schema::hasTable` guard on the validation-rules cleanup keeps the
* observer safe when this code runs against a database where commit 1's
* migration has not yet been applied (e.g. during the migration step
* itself, where the observer is registered before the table exists).
*/
final class FormFieldChildTablesCascadeObserver
{
public function deleted(FormField|FormFieldLibrary $owner): void
{
$ownerType = $owner instanceof FormField ? 'form_field' : 'form_field_library';
$ownerId = $owner->getKey();
DB::table('form_field_bindings')
->where('owner_type', $ownerType)
->where('owner_id', $ownerId)
->delete();
if (Schema::hasTable('form_field_validation_rules')) {
DB::table('form_field_validation_rules')
->where('owner_type', $ownerType)
->where('owner_id', $ownerId)
->delete();
}
}
}