|
4 | 4 | How to Generate Entities from an Existing Database
|
5 | 5 | ==================================================
|
6 | 6 |
|
7 |
| -.. caution:: |
8 |
| - |
9 |
| - The feature explained in this article doesn't work in modern Symfony |
10 |
| - applications that have no bundles. The workaround is to temporarily create |
11 |
| - a bundle. See `doctrine/doctrine#729`_ for details. Moreover, this feature |
12 |
| - to generate entities from existing databases will be completely removed in |
13 |
| - the next Doctrine version. |
14 |
| - |
15 | 7 | When starting work on a brand new project that uses a database, two different
|
16 | 8 | situations comes naturally. In most cases, the database model is designed
|
17 | 9 | and built from scratch. Sometimes, however, you'll start with an existing and
|
@@ -64,112 +56,41 @@ table fields.
|
64 | 56 |
|
65 | 57 | .. code-block:: terminal
|
66 | 58 |
|
67 |
| - $ php bin/console doctrine:mapping:import --force AppBundle xml |
| 59 | + $ php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity |
68 | 60 |
|
69 | 61 | This command line tool asks Doctrine to introspect the database and generate
|
70 |
| -the XML metadata files under the ``src/Resources/config/doctrine`` |
71 |
| -folder of your bundle. This generates two files: ``BlogPost.orm.xml`` and |
72 |
| -``BlogComment.orm.xml``. |
| 62 | +new PHP classes with annotation metadata into ``src/Entity``. This generates two |
| 63 | +files: ``BlogPost.php`` and ``BlogComment.php``. |
73 | 64 |
|
74 | 65 | .. tip::
|
75 | 66 |
|
76 |
| - It's also possible to generate the metadata files in YAML format by changing |
77 |
| - the last argument to ``yml``. |
| 67 | + It's also possible to generate the metadata files into XML or YAML: |
| 68 | + |
| 69 | + .. code-block:: terminal |
78 | 70 |
|
79 |
| -The generated ``BlogPost.orm.xml`` metadata file looks as follows: |
| 71 | + $ php bin/console doctrine:mapping:import App\\Entity xml --path=config/doctrine |
80 | 72 |
|
81 |
| -.. code-block:: xml |
| 73 | +Generating the Getters & Setters or PHP Classes |
| 74 | +----------------------------------------------- |
82 | 75 |
|
83 |
| - <?xml version="1.0" encoding="utf-8"?> |
84 |
| - <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> |
85 |
| - <entity name="App\Entity\BlogPost" table="blog_post"> |
86 |
| - <id name="id" type="bigint" column="id"> |
87 |
| - <generator strategy="IDENTITY"/> |
88 |
| - </id> |
89 |
| - <field name="title" type="string" column="title" length="100" nullable="false"/> |
90 |
| - <field name="content" type="text" column="content" nullable="false"/> |
91 |
| - <field name="createdAt" type="datetime" column="created_at" nullable="false"/> |
92 |
| - </entity> |
93 |
| - </doctrine-mapping> |
| 76 | +The generated PHP classes now have properties and annotation metadata, but they |
| 77 | +do *not* have any getter or setter methods. If you generated XML or YAML metadata, |
| 78 | +you don't even have the PHP classes! |
94 | 79 |
|
95 |
| -Once the metadata files are generated, you can ask Doctrine to build related |
96 |
| -entity classes by executing the following command. |
| 80 | +To generate the missing getter/setter methods (or to *create* the classes if neceesary), |
| 81 | +run: |
97 | 82 |
|
98 | 83 | .. code-block:: terminal
|
99 | 84 |
|
100 |
| - // generates entity classes with annotation mappings |
101 |
| - $ php bin/console doctrine:mapping:convert annotation ./src |
102 |
| -
|
103 |
| -.. caution:: |
104 |
| - |
105 |
| - If you want to use annotations, you must remove the XML (or YAML) files |
106 |
| - after running this command. This is necessary as |
107 |
| - :ref:`it is not possible to mix mapping configuration formats <doctrine-adding-mapping>` |
108 |
| - |
109 |
| -For example, the newly created ``BlogComment`` entity class looks as follow:: |
110 |
| - |
111 |
| - // src/Entity/BlogComment.php |
112 |
| - namespace App\Entity; |
113 |
| - |
114 |
| - use Doctrine\ORM\Mapping as ORM; |
115 |
| - |
116 |
| - /** |
117 |
| - * @ORM\Table(name="blog_comment") |
118 |
| - * @ORM\Entity |
119 |
| - */ |
120 |
| - class BlogComment |
121 |
| - { |
122 |
| - /** |
123 |
| - * @var integer $id |
124 |
| - * |
125 |
| - * @ORM\Column(name="id", type="bigint") |
126 |
| - * @ORM\Id |
127 |
| - * @ORM\GeneratedValue(strategy="IDENTITY") |
128 |
| - */ |
129 |
| - private $id; |
130 |
| - |
131 |
| - /** |
132 |
| - * @var string $author |
133 |
| - * |
134 |
| - * @ORM\Column(name="author", type="string", length=100, nullable=false) |
135 |
| - */ |
136 |
| - private $author; |
137 |
| - |
138 |
| - /** |
139 |
| - * @var text $content |
140 |
| - * |
141 |
| - * @ORM\Column(name="content", type="text", nullable=false) |
142 |
| - */ |
143 |
| - private $content; |
144 |
| - |
145 |
| - /** |
146 |
| - * @var datetime $createdAt |
147 |
| - * |
148 |
| - * @ORM\Column(name="created_at", type="datetime", nullable=false) |
149 |
| - */ |
150 |
| - private $createdAt; |
151 |
| - |
152 |
| - /** |
153 |
| - * @var BlogPost |
154 |
| - * |
155 |
| - * @ORM\ManyToOne(targetEntity="BlogPost") |
156 |
| - * @ORM\JoinColumn(name="post_id", referencedColumnName="id") |
157 |
| - */ |
158 |
| - private $post; |
159 |
| - } |
160 |
| - |
161 |
| -As you can see, Doctrine converts all table fields to pure private and annotated |
162 |
| -class properties. The most impressive thing is that it also discovered the |
163 |
| -relationship with the ``BlogPost`` entity class based on the foreign key constraint. |
164 |
| -Consequently, you can find a private ``$post`` property mapped with a ``BlogPost`` |
165 |
| -entity in the ``BlogComment`` entity class. |
| 85 | + // generates getter/setter methods |
| 86 | + $ php bin/console make:entity --regenerate App |
166 | 87 |
|
167 | 88 | .. note::
|
168 | 89 |
|
169 |
| - If you want to have a one-to-many relationship, you will need to add |
170 |
| - it manually into the entity or to the generated XML or YAML files. |
171 |
| - Add a section on the specific entities for one-to-many defining the |
172 |
| - ``inversedBy`` and the ``mappedBy`` pieces. |
| 90 | + If you want to have a OneToMany relationship, you will need to add |
| 91 | + it manually into the entity (e.g. add a ``comments`` property to ``BlogPost``) |
| 92 | + or to the generated XML or YAML files. Add a section on the specific entities |
| 93 | + for one-to-many defining the ``inversedBy`` and the ``mappedBy`` pieces. |
173 | 94 |
|
174 | 95 | The generated entities are now ready to be used. Have fun!
|
175 | 96 |
|
|
0 commit comments