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 Db Mapper Bundle Laravel Package

dimoussa/doctrine-db-mapper-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer:

composer require dimoussa/doctrine-db-mapper-bundle

Configure your MySQL connection in .env:

DATABASE_URL="mysql://user:password@localhost:3306/your_database"

First Use Case: Generate all entities from your database schema into a Laravel-compatible structure (e.g., src/Entity):

php artisan dbmapper:generate-entities src/Entity

Laravel Note: While this is a Symfony bundle, it can be integrated into Laravel via Symfony's console component or by using Laravel's Artisan command facade. For Laravel, ensure you have symfony/console installed and configure the bundle's commands in config/console.php.


Implementation Patterns

Core Workflow

  1. Initial Setup:

    • Run the generator to scaffold entities from an existing database.
    • Use --table=TABLE_NAME to generate a single entity (e.g., --table=users).
  2. Iterative Development:

    • Modify the database schema (e.g., add a column, create a table).
    • Regenerate entities with --merge to preserve custom logic:
      php artisan dbmapper:generate-entities src/Entity --merge --table=users
      
    • The bundle merges generated code with existing files, preserving:
      • Custom methods.
      • Non-ORM properties (e.g., $nonDoctrineField).
      • Interfaces, traits, and use statements.
  3. Safety Checks:

    • Preview schema changes before generation:
      php artisan dbmapper:generate-entities src/Entity --schema-preview
      
    • This outputs SQL Doctrine would execute to align the database, allowing you to validate changes.
  4. Interactive Schema Management:

    • Use the dbmapper:modify-entities command for a CLI-driven workflow to:
      • Add columns, modify relations, or preview SQL changes.
      • Apply changes incrementally without regenerating the entire schema.

Laravel Integration Tips

  • Artisan Command Registration: Add the bundle's commands to Laravel's App\Console\Kernel:

    protected $commands = [
        // ...
        \Dimoussa\DoctrineDbMapperBundle\Command\GenerateEntitiesCommand::class,
        \Dimoussa\DoctrineDbMapperBundle\Command\ModifyEntitiesCommand::class,
    ];
    
  • Doctrine Configuration: Ensure your config/database.php includes the Doctrine DSN format (e.g., mysql://user:pass@host/db). Laravel’s default .env format (e.g., DB_DATABASE=db) may require conversion for the bundle to work.

  • Entity Namespace: Specify the target namespace in the command (e.g., src/Entity) to match Laravel’s autoloading conventions.

  • Custom Merging Logic: Extend the bundle’s merging behavior by overriding its template files (located in vendor/dimoussa/doctrine-db-mapper-bundle/Resources/templates) or by creating a custom merge strategy.


Gotchas and Tips

Pitfalls

  1. Namespace Conflicts:

    • The bundle assumes entities are generated in a Symfony-style namespace (e.g., App\Entity). In Laravel, ensure the target directory (e.g., src/Entity) aligns with your composer.json autoloading:
      "autoload": {
          "psr-4": {
              "App\\": "src/"
          }
      }
      
    • If using a custom namespace (e.g., App\Models), configure the bundle’s namespace option in config/packages/dimoussa_doctrine_db_mapper.yaml:
      dimoussa_doctrine_db_mapper:
          namespace: App\Models
      
  2. Doctrine Event Listeners/Subscribers:

    • The bundle does not generate or preserve Doctrine event listeners (e.g., LifecycleCallbacks). Add these manually or extend the merge logic to include them.
  3. Composite Primary Keys:

    • ManyToMany association tables with composite keys (e.g., user_role with user_id + role_id) are handled automatically, but ensure the generated entities reflect the correct order of foreign keys in the database. Verify the id and inversedBy mappings in the generated code.
  4. Ignored Tables:

    • System tables (e.g., migrations, messenger_messages) are ignored by default. Customize the ignored_tables list in the config if needed:
      dimoussa_doctrine_db_mapper:
          ignored_tables: [migrations, custom_ignored_table]
      
  5. Database Synchronization:

    • The bundle auto-synchronizes the database after generation to fix cosmetic issues (e.g., index names, textlongtext). Disable this with --no-sync if you manage schema migrations separately:
      php artisan dbmapper:generate-entities src/Entity --no-sync
      
  6. PHP Types vs. Doctrine Types:

    • The bundle maps MySQL types to PHP types (e.g., VARCHAR(255)string). For unsupported types (e.g., ENUM), customize the type mapping in the config:
      dimoussa_doctrine_db_mapper:
          type_mapping:
              enum: string
      

Debugging Tips

  • Verbose Output: Use --verbose to debug generation issues:

    php artisan dbmapper:generate-entities src/Entity --verbose
    
  • Dry Run: Combine --schema-preview with --verbose to inspect SQL changes without applying them.

  • Merge Conflicts: If custom code is lost during merging, check the generated file’s diff against the original. The bundle prioritizes preserving:

    • Class docblocks.
    • Properties outside @ORM\ annotations.
    • Methods not related to Doctrine (e.g., custom accessors).

Extension Points

  1. Custom Templates: Override the bundle’s Twig templates (e.g., Entity.php.twig) to modify generated entity structure. Place custom templates in:

    resources/templates/dimoussa_doctrine_db_mapper/
    
  2. Pre/Post-Generation Hooks: Extend the GenerateEntitiesCommand to add custom logic before/after generation. Example:

    // app/Console/Commands/ExtendedGenerateEntities.php
    namespace App\Console\Commands;
    use Dimoussa\DoctrineDbMapperBundle\Command\GenerateEntitiesCommand;
    
    class ExtendedGenerateEntities extends GenerateEntitiesCommand
    {
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            // Custom logic before generation
            parent::execute($input, $output);
            // Custom logic after generation
        }
    }
    
  3. Relation Detection: Customize how the bundle detects relationships by extending the RelationDetector service. Bind your custom detector in a service provider:

    $this->app->extend('dimoussa_doctrine_db_mapper.relation_detector', function () {
        return new \App\Services\CustomRelationDetector();
    });
    
  4. Laravel Eloquent Integration: To use generated entities with Laravel’s Eloquent, add the HasFactory, SoftDeletes, or other traits manually after generation. The --merge flag preserves these additions.

  5. Testing: Use the --schema-preview flag in CI/CD pipelines to validate schema changes without modifying the database:

    php artisan dbmapper:generate-entities src/Entity --schema-preview
    

    Compare the output SQL against expected changes using tools like diff or custom scripts.

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.
terminal42/code-quality-tools
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