WS-5a commit 1 of 4 per ARCH-CONSOLIDATION-ADDENDUM-2026-04-24 Q3. Creates the relational home for what was form_fields.binding JSON and form_field_library.default_binding JSON. Owner discriminator is polymorphic morph (owner_type/owner_id) — the pattern the rest of WS-5 (5b validation_rules, 5d options) will reuse. Migration backfills rows from both JSON sources in a single transaction and is genuinely reversible (rollback reconstructs the JSON). Old columns remain in place until commit 3 has switched all readers. Pattern B (binding=null) is represented by absence of row. mode enum covers entity_owned / mirrored only. Cascade on owner delete via observer — bindings are physical state, not historical audit. FormFieldBindingScope enforces multi-tenancy via UNION over both owner chains (form_field → schema → org OR form_field_library → org) — Q2's declarative tenantScopeStrategy() can't walk morph parents. Tests: migration forward/back, morph relation, cascade observer, scope isolation, enum coverage. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
75 lines
1.8 KiB
PHP
75 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models\FormBuilder;
|
|
|
|
use App\Models\Organisation;
|
|
use App\Models\Scopes\OrganisationScope;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
|
|
final class FormFieldLibrary extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasUlids;
|
|
|
|
protected $table = 'form_field_library';
|
|
|
|
public string $organisationScopeColumn = 'organisation_id';
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope(new OrganisationScope());
|
|
}
|
|
|
|
protected $fillable = [
|
|
'organisation_id',
|
|
'name',
|
|
'slug',
|
|
'field_type',
|
|
'label',
|
|
'help_text',
|
|
'options',
|
|
'validation_rules',
|
|
'default_is_required',
|
|
'default_is_filterable',
|
|
'default_binding',
|
|
'translations',
|
|
'description',
|
|
'is_active',
|
|
];
|
|
|
|
/** @var array<string, string> */
|
|
protected $casts = [
|
|
'options' => 'array',
|
|
'validation_rules' => 'array',
|
|
'default_binding' => 'array',
|
|
'translations' => 'array',
|
|
'default_is_required' => 'bool',
|
|
'default_is_filterable' => 'bool',
|
|
'is_system' => 'bool',
|
|
'is_active' => 'bool',
|
|
'usage_count' => 'int',
|
|
];
|
|
|
|
public function organisation(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Organisation::class);
|
|
}
|
|
|
|
public function fields(): HasMany
|
|
{
|
|
return $this->hasMany(FormField::class, 'library_field_id');
|
|
}
|
|
|
|
public function bindings(): MorphMany
|
|
{
|
|
return $this->morphMany(FormFieldBinding::class, 'owner');
|
|
}
|
|
}
|