Skip to content

Commit 5080cfb

Browse files
committed
feature #9491 Doctrine reverse engineering updates for 4.0 (weaverryan)
This PR was squashed before being merged into the 4.0 branch (closes #9491). Discussion ---------- Doctrine reverse engineering updates for 4.0 This is waiting on https://github.com/doctrine/DoctrineBundle#791 to be in a tagged release. But, otherwise, I just tried the entire process with that patch. Works great. Commits ------- 61bdd26 Updating arg: we need to pass the full namespace (with double slashes) e8eb547 updating the doctrine reverse engineering for 4.0
2 parents 53b231b + 61bdd26 commit 5080cfb

File tree

1 file changed

+20
-99
lines changed

1 file changed

+20
-99
lines changed

doctrine/reverse_engineering.rst

Lines changed: 20 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
How to Generate Entities from an Existing Database
55
==================================================
66

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-
157
When starting work on a brand new project that uses a database, two different
168
situations comes naturally. In most cases, the database model is designed
179
and built from scratch. Sometimes, however, you'll start with an existing and
@@ -64,112 +56,41 @@ table fields.
6456

6557
.. code-block:: terminal
6658
67-
$ php bin/console doctrine:mapping:import --force AppBundle xml
59+
$ php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity
6860
6961
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``.
7364

7465
.. tip::
7566

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
7870
79-
The generated ``BlogPost.orm.xml`` metadata file looks as follows:
71+
$ php bin/console doctrine:mapping:import App\\Entity xml --path=config/doctrine
8072
81-
.. code-block:: xml
73+
Generating the Getters & Setters or PHP Classes
74+
-----------------------------------------------
8275

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!
9479

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:
9782

9883
.. code-block:: terminal
9984
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
16687
16788
.. note::
16889

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.
17394

17495
The generated entities are now ready to be used. Have fun!
17596

0 commit comments

Comments
 (0)