How Symfony create entity class from mysql database

My computer develop environment

1
2
3
4
5
6
7
8
9
10
$ cat composer.json
...
"require": {
"php": ">=5.5.9",
"symfony/symfony": "2.8.*",
...
},

$ mysqld -V
mysqld Ver 5.7.18 for Linux on x86_64 (MySQL Community Server (GPL))

In some case, you will meet a secondary development project based on mysql.It has a complete database and your new project bases on Symfony framework.
As you know, if there are entities mapping class will speed up your development. So this is why this tutorial exist.

First of all: this converter doesn’t support some basic MySQL data types by default, so you have to edit your app/config/config.yml file as follows:

Update Doctrine Configuration

1
2
3
4
5
doctrine:
dbal:
mapping_types:
enum: string
bit: integer

You might need to add some other types, too.
As Doctrine does not support any of these:

BIT, BINARY, VARBINARY, TINYBLOB, MEDIUMBLOB, BLOB, LONGBLOB, ENUM, SET, GEOMETRY, POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON.

Make sure all tables have primary keys

OK, that might sound obvious.
But I real had some tables without the key.

Generate the mapping file

1
$ php app/console doctrine:mapping:import --force AppBundle xml -vvv

Generate Entities under directory ‘src/AppBundle/Entity’

1
$ php app/console doctrine:mapping:convert annotation ./src

Generate the Get and Set method in entity

1
$ php app/console generate:doctrine:entities AppBundle

Ok, entities will all be done!

Share