bonusone/doctrine-reverse-engineering
Installation:
composer require bonusone/doctrine-reverse-engineering
The package auto-registers via Symfony’s symfony.bundle.
First Run: Generate entities/repositories for your default database connection:
php bin/console doctrine:reverse-engineering:generate
Outputs to src/Entity and src/Repository by default (configurable).
Verify:
Check generated files in src/Entity/ (e.g., User.php) and src/Repository/ (e.g., UserRepository.php).
Legacy DB Migration:
DATABASE_URL to your existing database.UserRepository::findAll()).Incremental Generation:
--connection to target specific databases (e.g., --connection=legacy_db).overwrite_existing: true cautiously).Custom Namespace/Path:
Override defaults in config/packages/doctrine_reverse_engineering.yaml:
doctrine_reverse_engineering:
entity_namespace: 'App\Domain\User\Entity'
repository_path: '%kernel.project_dir%/src/Domain/User/Repository'
CI/CD Integration: Add to deployment scripts to auto-generate entities on schema changes:
php bin/console doctrine:reverse-engineering:generate --overwrite
Doctrine Fixtures:
Use generated entities with doctrine:fixtures:load for test data.
Example: UserFixtures::load() referencing User entity.
Symfony Maker:
Extend generated entities with custom logic (e.g., add #[ORM\GeneratedValue] or traits).
API Platform:
Annotate entities with @ApiResource for auto-generated API endpoints:
#[ApiResource]
class User { ... }
Event Subscribers:
Hook into postGenerate events (if extended) to auto-register services or seed data.
Overwriting Files:
overwrite_existing: false prevents accidents but may require manual cleanup.--overwrite flag sparingly or set overwrite_existing: true in config.Namespace Collisions:
App\Entity. Conflicts arise if multiple bundles use the same namespace.entity_namespace per bundle (e.g., App\Bundle\Admin\Entity).Complex Schema Quirks:
?array instead of JsonEncoded).Doctrine Migrations:
doctrine:migrations:diff after generation to sync schema.Dry Run:
Use --dry-run (if available) or check var/log/dev.log for connection errors.
Example error:
[Doctrine\DBAL\Exception\ConnectionException] An exception occurred while executing 'SELECT ...': SQLSTATE[HY000] [2002] No such file or directory
Fix: Verify DATABASE_URL (e.g., mysql://user:pass@localhost/db).
Generated Code Issues:
use statements) often stems from malformed DB schema.doctrine:schema:validate.Partial Generation: Target specific tables by filtering in the generator (if supported) or manually edit the command’s logic.
Extension Points:
BonusOne\DoctrineReverseEngineering\Generator\Generator and binding it in services.yaml:
BonusOne\DoctrineReverseEngineering\Generator\Generator: '@app.custom_generator'
Template Customization:
Modify the Twig templates (located in the package’s Resources/views/) to change entity/repo structure (e.g., add #[ORM\Table(name: 'custom_name')]).
Performance: For large databases, generate entities in batches or during off-peak hours.
Testing:
Use the generator in a phpunit.xml setup to scaffold test entities:
php bin/console doctrine:reverse-engineering:generate --entity-namespace="App\Tests\Entity"
How can I help you explore Laravel packages today?