|
4 | 4 | How to Generate Entities from an Existing Database |
5 | 5 | ================================================== |
6 | 6 |
|
7 | | -When starting work on a brand new project that uses a database, two different |
8 | | -situations can occur. In most cases, the database model is designed |
9 | | -and built from scratch. Sometimes, however, you'll start with an existing and |
10 | | -probably unchangeable database model. Fortunately, Doctrine comes with a bunch |
11 | | -of tools to help generate model classes from your existing database. |
| 7 | +.. caution:: |
12 | 8 |
|
13 | | -.. note:: |
| 9 | + The ``doctrine:mapping:import`` command used to generate Doctrine entities |
| 10 | + from existing databases was deprecated by Doctrine in 2019 and it's no |
| 11 | + longer recommended to use it. |
14 | 12 |
|
15 | | - As the `Doctrine tools documentation`_ says, reverse engineering is a |
16 | | - one-time process to get started on a project. Doctrine is able to convert |
17 | | - approximately 70-80% of the necessary mapping information based on fields, |
18 | | - indexes and foreign key constraints. Doctrine can't discover inverse |
19 | | - associations, inheritance types, entities with foreign keys as primary keys |
20 | | - or semantical operations on associations such as cascade or lifecycle |
21 | | - events. Some additional work on the generated entities will be necessary |
22 | | - afterwards to design each to fit your domain model specificities. |
| 13 | + Instead, you can use the ``make:entity`` command from `Symfony Maker Bundle`_ |
| 14 | + to quickly generate the Doctrine entities of your application. |
23 | 15 |
|
24 | | -This tutorial assumes you're using a simple blog application with the following |
25 | | -two tables: ``blog_post`` and ``blog_comment``. A comment record is linked |
26 | | -to a post record thanks to a foreign key constraint. |
27 | | - |
28 | | -.. code-block:: sql |
29 | | -
|
30 | | - CREATE TABLE `blog_post` ( |
31 | | - `id` bigint(20) NOT NULL AUTO_INCREMENT, |
32 | | - `title` varchar(100) COLLATE utf8_unicode_ci NOT NULL, |
33 | | - `content` longtext COLLATE utf8_unicode_ci NOT NULL, |
34 | | - `created_at` datetime NOT NULL, |
35 | | - PRIMARY KEY (`id`) |
36 | | - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
37 | | -
|
38 | | - CREATE TABLE `blog_comment` ( |
39 | | - `id` bigint(20) NOT NULL AUTO_INCREMENT, |
40 | | - `post_id` bigint(20) NOT NULL, |
41 | | - `author` varchar(20) COLLATE utf8_unicode_ci NOT NULL, |
42 | | - `content` longtext COLLATE utf8_unicode_ci NOT NULL, |
43 | | - `created_at` datetime NOT NULL, |
44 | | - PRIMARY KEY (`id`), |
45 | | - KEY `blog_comment_post_id_idx` (`post_id`), |
46 | | - CONSTRAINT `blog_post_id` FOREIGN KEY (`post_id`) REFERENCES `blog_post` (`id`) ON DELETE CASCADE |
47 | | - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
48 | | -
|
49 | | -Before diving into the recipe, be sure your database connection parameters are |
50 | | -correctly set up in the ``.env`` file (or ``.env.local`` override file). |
51 | | - |
52 | | -The first step towards building entity classes from an existing database |
53 | | -is to ask Doctrine to introspect the database and generate the corresponding |
54 | | -metadata files. Metadata files describe the entity class to generate based on |
55 | | -table fields. |
56 | | - |
57 | | -.. code-block:: terminal |
58 | | -
|
59 | | - $ php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity |
60 | | -
|
61 | | -This command line tool asks Doctrine to introspect the database and generate |
62 | | -new PHP classes with annotation metadata into ``src/Entity``. This generates two |
63 | | -files: ``BlogPost.php`` and ``BlogComment.php``. |
64 | | - |
65 | | -.. tip:: |
66 | | - |
67 | | - It's also possible to generate the metadata files into XML or eventually into YAML: |
68 | | - |
69 | | - .. code-block:: terminal |
70 | | -
|
71 | | - $ php bin/console doctrine:mapping:import "App\Entity" xml --path=config/doctrine |
72 | | -
|
73 | | - In this case, make sure to adapt your mapping configuration accordingly: |
74 | | - |
75 | | - .. code-block:: yaml |
76 | | -
|
77 | | - # config/packages/doctrine.yaml |
78 | | - doctrine: |
79 | | - # ... |
80 | | - orm: |
81 | | - # ... |
82 | | - mappings: |
83 | | - App: |
84 | | - is_bundle: false |
85 | | - type: xml # "yml" is marked as deprecated for doctrine v2.6+ and will be removed in v3 |
86 | | - dir: '%kernel.project_dir%/config/doctrine' |
87 | | - prefix: 'App\Entity' |
88 | | - alias: App |
89 | | -
|
90 | | -Generating the Getters & Setters or PHP Classes |
91 | | ------------------------------------------------ |
92 | | - |
93 | | -The generated PHP classes now have properties and annotation metadata, but they |
94 | | -do *not* have any getter or setter methods. If you generated XML or YAML metadata, |
95 | | -you don't even have the PHP classes! |
96 | | - |
97 | | -To generate the missing getter/setter methods (or to *create* the classes if necessary), |
98 | | -run: |
99 | | - |
100 | | -.. code-block:: terminal |
101 | | -
|
102 | | - // generates getter/setter methods for all Entities |
103 | | - $ php bin/console make:entity --regenerate App |
104 | | -
|
105 | | - // generates getter/setter methods for one specific Entity |
106 | | - $ php bin/console make:entity --regenerate "App\Entity\Country" |
107 | | -
|
108 | | -.. note:: |
109 | | - |
110 | | - If you want to have a OneToMany relationship, you will need to add |
111 | | - it manually into the entity (e.g. add a ``comments`` property to ``BlogPost``) |
112 | | - or to the generated XML or YAML files. Add a section on the specific entities |
113 | | - for one-to-many defining the ``inversedBy`` and the ``mappedBy`` pieces. |
114 | | - |
115 | | -The generated entities are now ready to be used. Have fun! |
116 | | - |
117 | | -.. _`Doctrine tools documentation`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/tools.html#reverse-engineering |
| 16 | +.. _`Symfony Maker Bundle`: https://symfony.com/bundles/SymfonyMakerBundle/current/index.html |
0 commit comments