Skip to content

Conversation

@leek
Copy link

@leek leek commented Oct 16, 2025

This PR adds support for storing custom field values directly in entity model columns instead of the custom_field_values table. This is particularly useful for system-defined fields that map to existing database columns while maintaining the custom fields UI for managing field metadata.

Features Added

1. Entity Column Storage Flag
  • Added uses_entity_column boolean flag to custom_fields table
  • When enabled, field values are stored directly in the entity model's column (matching the field code)
  • Regular custom fields continue to use the custom_field_values table
2. Option Value/Label Separation
  • Added value column to custom_field_options table
  • Allows storing distinct values (e.g., 'M') and labels (e.g., 'Male') for select/choice fields
  • Backward compatible: falls back to option id when value is null
  • Particularly useful for enum-based fields with specific database values
4. UI Enhancements
  • Added "Store in Entity Column" toggle in field management form
  • Dynamic options table showing either "Value" or "Color" columns based on settings
  • System-defined fields can now have their label edited (while keeping code/type locked)
image

Use Case Example

  // Create a system-defined field that maps to the 'sex' column on Patient model
  $field = $this->migrator->new(
      model: Patient::class,
      fieldData: new CustomFieldData(
          name: 'Sex',
          code: 'sex',
          type: 'select',
          section: $section,
          systemDefined: true,
          usesEntityColumn: true,  // ← Stores in patients.sex column
      )
  );

  $field->options([
      ['value' => 'M', 'label' => 'Male'],
      ['value' => 'F', 'label' => 'Female'],
  ]);

  $field->create();

Breaking Changes

None - this is a fully backward-compatible addition. Existing fields without uses_entity_column set will continue working as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant