comunedifirenze/doctrine2-exporter
Exporter that converts MySQL Workbench .mwb models into Doctrine 2 schemas. Install via Composer and run the CLI to generate YAML schema or annotation-based entities, with options for namespaces, repositories, relations, nullability, cascades, and more.
Install the Package
composer require --dev mysql-workbench-schema-exporter/doctrine2-exporter
This installs both the exporter and its dependency (mysql-workbench-schema-exporter).
Locate the CLI Tool The exporter is available at:
vendor/bin/mysql-workbench-schema-export
First Use Case: Export a .mwb File to Doctrine YAML
vendor/bin/mysql-workbench-schema-export \
--input=path/to/schema.mwb \
--output=app/config/doctrine/Entity.yaml \
--format=doctrine2-yaml \
--entityNamespace=App\Entity
{d:unidirectional}true)..mwb to Doctrine EntitiesDefine Configuration
Use a config/doctrine-exporter.php file to centralize settings:
return [
'format' => 'doctrine2-annotation',
'entityNamespace' => 'App\Entity',
'bundleNamespace' => 'AppBundle',
'useAutomaticRepository' => true,
'nullableAttribute' => 'auto',
'quoteIdentifierStrategy' => 'auto',
'generateExtendableEntity' => false,
];
Integrate into Build Process
Add a script to package.json or composer.json to auto-generate entities on post-install:
{
"scripts": {
"post-install-cmd": "php vendor/bin/mysql-workbench-schema-export --input=db/schema.mwb --output=src/Entity --format=doctrine2-annotation --config=config/doctrine-exporter.php"
}
}
Leverage Model Comments for Custom Logic Annotate tables/foreign keys in MySQL Workbench to override defaults:
{d:unidirectional}true{/d:unidirectional} // Force unidirectional relation
{d:cascade}persist,remove{/d:cascade} // Custom cascade rules
Symfony Projects:
Place generated entities in src/Entity and update config/packages/doctrine.yaml to auto-discover them.
doctrine:
orm:
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
ZF2 Projects:
Use the doctrine2-annotation-zf2 formatter to generate input filter classes alongside entities.
CI/CD Pipelines:
Trigger exports in GitHub Actions/GitLab CI to sync .mwb changes with code:
- name: Export Doctrine Entities
run: php vendor/bin/mysql-workbench-schema-export --input=db/latest.mwb --output=src/Entity --format=doctrine2-annotation
Namespace Conflicts
entityNamespace isn’t unique.App\Generated\Entity) and exclude generated files from autoloading via composer.json:
"autoload-exclude": ["src/Generated/**"]
Reserved Keywords in Identifiers
class, table) may break parsing.quoteIdentifierStrategy: always in config or rename identifiers in MySQL Workbench.Many-to-Many Guessing
{d:m2m}false to table comments for false positives.Annotation vs. YAML
GeneratedValue strategies).doctrine2-annotation for new projects.Dry Runs:
Use --dry-run to preview output without writing files:
vendor/bin/mysql-workbench-schema-export --input=schema.mwb --format=doctrine2-annotation --dry-run
Verbose Logging: Enable debug mode for detailed parsing logs:
vendor/bin/mysql-workbench-schema-export --debug
Partial Exports:
Export specific tables using --tables=table1,table2 to isolate issues.
Custom Formatters
Extend the exporter by creating a new formatter class (e.g., for XML or PHP arrays) by implementing MysqlWorkbenchSchemaExporter\FormatterInterface.
Post-Processing Hooks
Use Symfony’s EventDispatcher or a custom script to modify generated files:
// Example: Add a timestamp to all entities
$entities = file_get_contents('src/Entity/User.php');
$entities = preg_replace('/class User/', 'class User { public function getCreatedAt() { return new \DateTime(); }', $entities);
file_put_contents('src/Entity/User.php', $entities);
Template Overrides
Replace default Doctrine annotations/templates by extending the Doctrine2AnnotationFormatter class and overriding methods like generateProperty().
generatedValueStrategy:
Set to AUTO (Doctrine default) or explicitly define (e.g., IDENTITY, SEQUENCE).
'generatedValueStrategy' => 'SEQUENCE',
extendsClass:
Use to enforce a base entity (e.g., App\Entity\BaseEntity) for shared methods:
'extendsClass' => 'App\Entity\BaseEntity',
propertyTypehint:
Enable for stricter typing (PHP 7.4+):
'propertyTypehint' => true,
Outputs:
/** @ORM\ManyToOne(targetEntity="App\Entity\User") */
private ?User $owner = null;
How can I help you explore Laravel packages today?