comunedifirenze/mysql-workbench-schema-exporter
Converts MySQL Workbench .mwb models into other schemas via formatter/exporter plugins, including Doctrine 1/2 (YAML/annotations), Propel (XML/YAML), Zend Framework, Sencha ExtJS models, and Node Sequelize. Includes a CLI; install via Composer.
Install the Package
composer require --dev mysql-workbench-schema-exporter/mysql-workbench-schema-exporter
For a specific exporter (e.g., Doctrine 2):
composer require --dev mysql-workbench-schema-exporter/doctrine2-exporter
Export via CLI Run the CLI tool directly:
vendor/bin/mysql-workbench-schema-export --export=doctrine2-annotation path/to/schema.mwb ./output_dir
Or use a config file (export.json) for reusable settings:
vendor/bin/mysql-workbench-schema-export --config=export.json path/to/schema.mwb
First Use Case
Convert a .mwb file (MySQL Workbench model) to Doctrine 2 YAML/Annotations for Laravel:
vendor/bin/mysql-workbench-schema-export --export=doctrine2-annotation --saveconfig schema.mwb
This generates entity classes with annotations (e.g., @ORM\Entity) in ./temp/.
Schema-to-Entity Generation
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
$driver = new AnnotationDriver(new \Doctrine\Common\Annotations\AnnotationReader(), [__DIR__.'/generated_entities']);
$config->setMetadataDriverImpl($driver);
app/Entities or a dedicated database/doctrine folder.CI/CD Integration
composer.json to auto-generate entities on post-install:
"scripts": {
"post-install-cmd": [
"php vendor/bin/mysql-workbench-schema-export --config=export.json resources/schema.mwb database/doctrine"
]
}
.mwb files and ignore generated files (add to .gitignore):
/database/doctrine/*
Dynamic Configuration
config/database.php):
$params = config('mysql_workbench.exporter');
$exporter = new \MwbExporter\Doctrine2\Exporter($params);
$exporter->export($mwbFile, $outputDir);
Hybrid Workflows
.mwb.php artisan make:migration or tools like DoctrineMigrations.Testing
vendor/bin/mysql-workbench-schema-export --export=doctrine2-annotation --dir=tests/Entities schema_test.mwb
Laravel Service Provider
Register the exporter as a service in AppServiceProvider:
public function register()
{
$this->app->singleton('mwb.exporter', function ($app) {
return new \MwbExporter\Doctrine2\Exporter(config('mysql_workbench.exporter'));
});
}
Call it via:
$exporter = app('mwb.exporter');
$exporter->export(storage_path('app/schema.mwb'), base_path('database/doctrine'));
Artisan Command Create a custom command for on-demand exports:
php artisan make:command ExportMwbSchema
// app/Console/Commands/ExportMwbSchema.php
public function handle()
{
$exporter = new \MwbExporter\Doctrine2\Exporter($this->getExporterConfig());
$exporter->export($this->argument('file'), $this->argument('destination'));
}
Run with:
php artisan export:mwb-schema schema.mwb database/doctrine
Event Listeners
Trigger exports after .mwb file changes (e.g., via filesystem events):
// app/Providers/EventServiceProvider.php
protected $listen = [
'filesystem.updated' => ['App\Listeners\ExportMwbSchema'],
];
Namespace Collisions
entityNamespace and bundleNamespace in config.entityNamespace: "App\\Entities" in export.json to avoid Class 'User' not found errors.Many-to-Many Relations
enhanceManyToManyDetection: false). Enable with:
"params": {
"enhanceManyToManyDetection": true
}
user_role vs. role_user).Reserved Keywords
order, group) in table/column names may break generated code.asIsUserDatatypePrefix or manually rename in MySQL Workbench.File Overwrites
backupExistingFile: true creates backups (e.g., User.php.bak), but may clutter directories.backupExistingFile: false in CI/CD or use --no-backup (if supported).PHP Version Mismatches
Zend Framework 1 Exporter
zend1-exporter is outdated and may not work with modern Laravel. Avoid unless maintaining legacy code.Sencha Exporters
Enable Logging
Add to export.json:
"logToConsole": true,
"logFile": "export.log"
Check for errors like:
[ERROR] Table 'order' could not be exported: Reserved keyword.
Validate .mwb Files
.mwb files may cause silent failures. Test with the Sakila sample database.--list-exporter to verify available exporters.Generated Code Issues
php artisan doctrine:schema:validate to check for syntax errors.Permission Errors
www-data) has write access to the output directory:
chmod -R 775 database/doctrine
Template Configs
Create reusable export.json templates for different environments (dev/staging/prod):
// export.dev.json
{
"export": "doctrine2-annotation",
"dir": "database/doctrine_dev",
"params": {
"indentation": 4,
"useTabs": false,
"skipPluralNameChecking": true
}
}
Partial Exports
Use {MwbExporter:category} comments to export only specific tables:
-- In MySQL Workbench: Right-click table > Properties > Comment
{MwbExporter:category}admin{/MwbExporter:category}
Then filter in config:
"exportOnlyTableCategorized": "admin"
Custom Filename Patterns
Override default filenames (e.g., %entity%_%table%.php) for clarity:
"filename": "%entity%Entity.php"
Laravel Eloquent Compatibility Post-process generated entities to add Laravel-specific traits:
// After export, replace:
// @ORM\Entity
// With:
// @ORM\Entity(repositoryClass="App\Repositories\UserRepository")
Git Submodules
Store .mwb files in a submodule to share across projects:
git submodule add https://github.com/your-team/db-schema.git resources/schema
6
How can I help you explore Laravel packages today?