From MySQL-Workbench to Sonata-Admin


Design your Database inside MySQL-Workbench and get a nearly completely auto generated Sonata Admin from your MySQL-Workbench schemata.


Working as a web-developer I was mostly confronted to create a unique and custom data model for the customer. I started to use MySQL-Workbench to design my data structure and followed on that I looked for a way to get my created structure directly into PHP models and an auto generated database.

Years ago for the bmw motorcyle community (Zend Framework) I started with a simple macro to just create to create the database structure. After that I used Doctrine to generate my models. So I used it only as a project start.

With an auction platform for the “Österreichische Bundesforste AG” (Agavi) I found a nice schema exporter for the MySQL-Workbench. After some testing I could make some steps further. With the yaml export and the doctrine “generate-migrations-diff” I could also auto generate migrations after changing my data model inside MySQL-Workbench.

Now with Symfony I am still using the schema exporter from johmue. I created a fork to fit my needs for my Symfony projects and it fits perfectly to get an easy and quick way from the MySQL-Workbench to a Sonata-Admin. And I created a Bundle the integrate the exporter into Symfony.



Install Symfony:

Install the MySQLWorkbench-Exporter and the Exporter-Bundle:

Configure Exporter Bundle:


schema_name here refers to name of the Workbench file without the extension (mwb). Workbench files are saved in the Resources/workbench/ directory as *.mwb inside your bundle.

 Create your schema with MySQL-Workbench:

I think the MySQL Workbench is pretty much self explaining. Create your tables and relations and than save your file to your Bundle /Ressources/workbench/schema_name.mwb

Export your schema and create your database tables:

Export the schema:

Check the changes to your database:

Update your database:

Install Sonata Admin:

See the offical dokumentation:

I am using the SonataDoctrineORMAdminBundle.

Follow to Step 2.1 and stop after this step (2.1). You can use the sonata generator for the following steps.

Create Admin classes with the generator:

I suggest to create an extra bundle for you admin generator.
e.g. YourProjectAdminBundle.

To create the admin classes you can use this command:

Run your Sonata Admin and get lucky:

Browse to your site with /admin.

As you can see, the admin generator does not auto include your references. You have to add in on your own. And the ID for the edit mask should be removed.

How to change your Sonata Admin?

  1. Make your changes inside the MySQL-Workbench
  2. Export your entities
  3. Update your database
  4. Recreate your Sonata Admin or manually add your new fields (or remove.. or whatever)
  5. get lucky (again)

Questions? Errors?

Please use the comments!