feat(form-builder): form_field_bindings table + polymorphic owner + cascade observer
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>
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
|
||||
use App\Enums\FormBuilder\FormFieldBindingMode;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormFieldBinding> */
|
||||
final class FormFieldBindingFactory extends Factory
|
||||
{
|
||||
protected $model = FormFieldBinding::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'owner_type' => 'form_field',
|
||||
'owner_id' => FormField::factory(),
|
||||
'target_entity' => 'person',
|
||||
'target_attribute' => 'email',
|
||||
'mode' => FormFieldBindingMode::EntityOwned->value,
|
||||
'sync_direction' => null,
|
||||
'merge_strategy' => FormFieldBindingMergeStrategy::Overwrite->value,
|
||||
'trust_level' => 50,
|
||||
'is_identity_key' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function forField(FormField $field): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'owner_type' => 'form_field',
|
||||
'owner_id' => $field->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function forLibrary(FormFieldLibrary $library): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'owner_type' => 'form_field_library',
|
||||
'owner_id' => $library->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function entityOwned(string $entity, string $attribute): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'target_entity' => $entity,
|
||||
'target_attribute' => $attribute,
|
||||
'mode' => FormFieldBindingMode::EntityOwned->value,
|
||||
'sync_direction' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function mirrored(string $entity, string $attribute, string $syncDirection = 'write_on_submit'): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'target_entity' => $entity,
|
||||
'target_attribute' => $attribute,
|
||||
'mode' => FormFieldBindingMode::Mirrored->value,
|
||||
'sync_direction' => $syncDirection,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldBindingMode;
|
||||
use App\Enums\FormBuilder\FormFieldDisplayWidth;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -73,4 +75,30 @@ final class FormFieldFactory extends Factory
|
||||
{
|
||||
return $this->state(fn () => ['is_filterable' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach an entity-binding row in `form_field_bindings` after the field
|
||||
* is persisted. Use this instead of populating the legacy `binding` JSON
|
||||
* column — which WS-5a will drop in commit 3.
|
||||
*/
|
||||
public function withEntityBinding(
|
||||
string $entity,
|
||||
string $attribute,
|
||||
FormFieldBindingMode $mode = FormFieldBindingMode::EntityOwned,
|
||||
?string $syncDirection = null,
|
||||
): static {
|
||||
return $this->afterCreating(function (FormField $field) use ($entity, $attribute, $mode, $syncDirection): void {
|
||||
FormFieldBinding::factory()
|
||||
->forField($field)
|
||||
->state([
|
||||
'target_entity' => $entity,
|
||||
'target_attribute' => $attribute,
|
||||
'mode' => $mode->value,
|
||||
'sync_direction' => $mode === FormFieldBindingMode::Mirrored
|
||||
? ($syncDirection ?? 'write_on_submit')
|
||||
: null,
|
||||
])
|
||||
->create();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldBindingMode;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
@@ -45,4 +47,29 @@ final class FormFieldLibraryFactory extends Factory
|
||||
{
|
||||
return $this->state(fn () => ['is_system' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a binding row in `form_field_bindings` after the library entry
|
||||
* is persisted. Replaces the legacy `default_binding` JSON column.
|
||||
*/
|
||||
public function withDefaultBinding(
|
||||
string $entity,
|
||||
string $attribute,
|
||||
FormFieldBindingMode $mode = FormFieldBindingMode::EntityOwned,
|
||||
?string $syncDirection = null,
|
||||
): static {
|
||||
return $this->afterCreating(function (FormFieldLibrary $library) use ($entity, $attribute, $mode, $syncDirection): void {
|
||||
FormFieldBinding::factory()
|
||||
->forLibrary($library)
|
||||
->state([
|
||||
'target_entity' => $entity,
|
||||
'target_attribute' => $attribute,
|
||||
'mode' => $mode->value,
|
||||
'sync_direction' => $mode === FormFieldBindingMode::Mirrored
|
||||
? ($syncDirection ?? 'write_on_submit')
|
||||
: null,
|
||||
])
|
||||
->create();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user