Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Enum Bundle Laravel Package

fresh/doctrine-enum-bundle

Symfony bundle adding ENUM type support to Doctrine ORM/DBAL. Register custom enum types and map them to entity fields for safer, consistent values across databases. Works with modern Symfony/Doctrine versions and common platforms like PostgreSQL, MySQL, SQLite, and MSSQL.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Doctrine-Centric Design: The bundle is a perfect fit for Symfony/Doctrine applications where ENUM types are needed for database columns. It bridges the gap between PHP enums (or string/integer representations) and native database ENUM constraints, enforcing data integrity at both the application and database layers.
  • Symfony Ecosystem Alignment: Integrates natively with Symfony’s:
    • Dependency Injection: Auto-configures via Flex.
    • Doctrine Events: Supports migration hooks (e.g., postGenerateSchemaCommand).
    • Twig/Forms: Provides filters and form types for ENUM values (e.g., enum_values Twig filter, EnumType form field).
  • Database Abstraction: While officially supporting PostgreSQL/MySQL/SQLite/MSSQL, the bundle abstracts ENUM handling via Doctrine’s DBAL, reducing vendor-lock risks. Custom backends (e.g., for MSSQL) are extensible.
  • Type Safety: Enforces compile-time checks for ENUM values (e.g., UserStatus::ACTIVE vs. "active"), reducing runtime errors and improving IDE support (PhpStorm, VSCode).

Integration Feasibility

  • Low-Coupling Design: Adds ENUM support without modifying core Doctrine or Symfony logic. Entities use standard Doctrine attributes (#[EnumType]) or annotations, with minimal additional configuration.
  • Migration Path:
    • Greenfield Projects: Install via Composer (fresh/doctrine-enum-bundle) and configure in bundles.php. ENUM columns are created automatically during schema generation.
    • Legacy Projects: Supports incremental adoption via:
      • Doctrine Migrations: Bundle provides hooks to alter existing columns (e.g., ALTER TABLE user ADD CONSTRAINT enum_user_status CHECK (status IN ('active', 'inactive'))).
      • Backward Compatibility: Works alongside existing string/integer fields during transition.
  • Form/Templating: Reduces boilerplate for ENUM handling in:
    • Symfony Forms: EnumType form field auto-generates choices from ENUM constants.
    • Twig: enum_values() filter renders human-readable labels (e.g., {% set statusLabel = enum_values(order.status) %}).
  • Testing: Includes PHPUnit tests and CI pipelines (Scrutinizer, CodeCov), ensuring reliability.

Technical Risk

  • PHP/Symfony Version Lock: Requires PHP 8.4+ and Symfony 7.4+ (or specific minor versions). Projects on older stacks (e.g., PHP 8.1, Symfony 6.2) must:
    • Downgrade to v11.2.* (supports PHP 8.2, Symfony 6.4).
    • Or upgrade dependencies (risk: breaking changes in Doctrine ORM 3.x).
  • Database Limitations:
    • MSSQL: ENUM support is experimental (requires custom DBAL types). Test thoroughly if using SQL Server.
    • Legacy Databases: Older MySQL/PostgreSQL versions may lack ENUM support or require schema migrations.
  • Performance Overhead:
    • ENUM Columns: Native database ENUMs are efficient, but custom backends (e.g., for MSSQL) may introduce slight overhead.
    • Twig Filters: enum_values() requires runtime reflection to map ENUM constants to labels, adding minimal CPU cost.
  • Learning Curve:
    • Doctrine Attributes: Teams unfamiliar with PHP 8.1+ attributes (#[EnumType]) may need training.
    • Migration Complexity: Altering existing columns (e.g., VARCHARENUM) requires downtime or careful transaction handling.
  • Edge Cases:
    • NULL Values: Requires explicit configuration (nullable: true in EnumType).
    • Dynamic Enums: Not suitable for runtime-defined ENUM values (use jsonb or array columns instead).
    • Multi-Language Labels: Labels are hardcoded in ENUM classes; i18n requires additional logic (e.g., translation service).

Key Questions for TPM

  1. Stack Compatibility:
    • Are we on PHP 8.4+ and Symfony 7.4+? If not, what’s the upgrade path?
    • Which databases are in use? (MSSQL requires extra validation.)
  2. Adoption Strategy:
    • Should we pilot with a single entity (e.g., OrderStatus) before full rollout?
    • How will we handle legacy columns (e.g., VARCHARENUM migrations)?
  3. Team Readiness:
    • Does the team have experience with Doctrine attributes or Symfony forms?
    • Are developers comfortable with database schema changes?
  4. Performance:
    • Will ENUM columns impact query performance (e.g., joins, indexes)?
    • How will we monitor database size (ENUMs can bloat storage if overused)?
  5. Alternatives:
    • Could we use Doctrine Extensions (e.g., Stof\DoctrineExtensionsBundle) for similar functionality?
    • Is a custom ENUM solution (e.g., abstract base class) feasible for our use case?

Integration Approach

Stack Fit

  • Primary Use Case: Ideal for Symfony + Doctrine ORM applications where:
    • ENUM types are used for state machines (e.g., OrderStatus, UserRole).
    • Data integrity is critical (e.g., payment gateways, workflows).
    • Twig/Symfony Forms need ENUM-aware rendering.
  • Secondary Use Case: Projects migrating from string/integer flags to type-safe enums.
  • Non-Fit Scenarios:
    • Non-Symfony PHP: Laravel, Slim, or raw PHP (use native enums or custom solutions).
    • NoSQL: MongoDB, Elasticsearch (use string + validation).
    • Dynamic Enums: Use jsonb or array columns instead.

Migration Path

  1. Preparation Phase:
    • Audit Dependencies: Verify PHP/Symfony/Doctrine versions match bundle requirements.
    • Backup Database: Ensure schema backups exist before migrations.
    • Team Training: Document Doctrine attributes (#[EnumType]) and migration steps.
  2. Pilot Phase:
    • Select a Non-Critical Entity: E.g., UserRole or OrderStatus.
    • Add ENUM Support:
      // src/Entity/UserRole.php
      use Fresh\DoctrineEnumBundle\Attributes\EnumType;
      
      #[EnumType]
      private UserRoleEnum $role;
      
    • Update Migrations: Use Doctrine’s migration tool to alter the column:
      php bin/console make:migration
      
    • Test Forms/Twig: Verify EnumType form fields and Twig filters work.
  3. Full Rollout:
    • Batch by Feature: Prioritize entities tied to new features (avoid legacy refactoring).
    • Database Migrations: Run in maintenance windows or use zero-downtime strategies (e.g., shadow columns).
    • Deprecate Old Fields: Replace VARCHAR/INT fields with ENUMs in new code.
  4. Post-Migration:
    • Monitor Performance: Check query logs for ENUM-related slowdowns.
    • Update Documentation: Reflect ENUM usage in API contracts and frontend specs.

Compatibility

  • Doctrine ORM: Tested with ORM 2.12+ and 3.x. Ensure doctrine/doctrine-bundle is up-to-date.
  • Database Drivers:
    • PostgreSQL/MySQL/SQLite: Native ENUM support (no config needed).
    • MSSQL: Requires custom DBAL type (test thoroughly).
  • Symfony Components:
    • Forms: EnumType integrates with Symfony’s form system.
    • Twig: enum_values filter works with Twig 2.12+.
    • Validator: Supports validation constraints (e.g., @Assert\Type).
  • Legacy Code:
    • Annotations: Supports @Enum for older Symfony versions (deprecated in favor of attributes).
    • String/Integer Fallback: Can coexist during migration (e.g., private ?string $legacyStatus).

Sequencing

  1. Critical Path:
    • Step 1: Install bundle and configure bundles.php.
    • Step 2: Add #[EnumType] to a pilot entity.
    • Step 3: Run migrations and test CRUD operations.
    • Step 4: Integrate with forms/templating.
  2. Parallel Tasks:
    • Documentation: Update API specs to reflect ENUM types.
    • Frontend: Align dropdowns/selects with new ENUM values.
    • CI/CD: Add tests for ENUM validation (e.g., PHPUnit constraints).
  3. Risk Mitigation:
    • Rollback Plan: Keep old `V
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor