dimoussa/doctrine-db-mapper-bundle
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.
Initial Setup:
--table=TABLE_NAME to generate a single entity (e.g., --table=users).Iterative Development:
--merge to preserve custom logic:
php artisan dbmapper:generate-entities src/Entity --merge --table=users
$nonDoctrineField).use statements.Safety Checks:
php artisan dbmapper:generate-entities src/Entity --schema-preview
Interactive Schema Management:
dbmapper:modify-entities command for a CLI-driven workflow to:
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.
Namespace Conflicts:
App\Entity). In Laravel, ensure the target directory (e.g., src/Entity) aligns with your composer.json autoloading:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
App\Models), configure the bundle’s namespace option in config/packages/dimoussa_doctrine_db_mapper.yaml:
dimoussa_doctrine_db_mapper:
namespace: App\Models
Doctrine Event Listeners/Subscribers:
LifecycleCallbacks). Add these manually or extend the merge logic to include them.Composite Primary Keys:
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.Ignored Tables:
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]
Database Synchronization:
text → longtext). Disable this with --no-sync if you manage schema migrations separately:
php artisan dbmapper:generate-entities src/Entity --no-sync
PHP Types vs. Doctrine Types:
VARCHAR(255) → string). For unsupported types (e.g., ENUM), customize the type mapping in the config:
dimoussa_doctrine_db_mapper:
type_mapping:
enum: string
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:
@ORM\ annotations.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/
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
}
}
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();
});
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.
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.
How can I help you explore Laravel packages today?