Skip to content

Commit 53b231b

Browse files
committed
feature #9487 Document Updated make:entity command (weaverryan)
This PR was squashed before being merged into the 4.0 branch (closes #9487). Discussion ---------- Document Updated make:entity command Hi guys! The make:entity command in MakerBundle 1.3 is quite a bit more powerful - it interactively asks for fields (like `doctrine:generate:entity`), but also updating existing entities, supports relationships and generates adders/removers that synchronize the owning side of a relationship. I've still included what the generated code looks like, because I want users to know the command is optional, and also to see what the code looks like when they're reading. Cheers! Commits ------- 5569400 Document Updated make:entity command
2 parents 355fc81 + 5569400 commit 53b231b

File tree

2 files changed

+225
-195
lines changed

2 files changed

+225
-195
lines changed

doctrine.rst

Lines changed: 114 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ code:
2323

2424
.. code-block:: terminal
2525
26-
composer require doctrine maker
26+
composer require doctrine
27+
composer require maker --dev
2728
2829
Configuring the Database
2930
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -69,14 +70,50 @@ Creating an Entity Class
6970

7071
Suppose you're building an application where products need to be displayed.
7172
Without even thinking about Doctrine or databases, you already know that
72-
you need a ``Product`` object to represent those products. Use the ``make:entity``
73-
command to create this class for you:
73+
you need a ``Product`` object to represent those products.
74+
75+
.. _doctrine-adding-mapping:
76+
77+
You can use the ``make:entity`` command to create this class and any fields you
78+
need. The command will ask you some questions - answer them like done below:
7479

7580
.. code-block:: terminal
7681
77-
$ php bin/console make:entity Product
82+
$ php bin/console make:entity
83+
84+
Class name of the entity to create or update:
85+
> Product
86+
87+
New property name (press <return> to stop adding fields):
88+
> name
89+
90+
Field type (enter ? to see all types) [string]:
91+
> string
92+
93+
Field length [255]:
94+
> 255
95+
96+
Can this field be null in the database (nullable) (yes/no) [no]:
97+
> no
98+
99+
New property name (press <return> to stop adding fields):
100+
> price
78101
79-
You now have a new ``src/Entity/Product.php`` file::
102+
Field type (enter ? to see all types) [string]:
103+
> integer
104+
105+
Can this field be null in the database (nullable) (yes/no) [no]:
106+
> no
107+
108+
New property name (press <return> to stop adding fields):
109+
>
110+
(press enter again to finish)
111+
112+
.. versionadded::
113+
The interactive behavior of the ``make:entity`` command was introduced
114+
in MakerBundle 1.3.
115+
116+
Woh! You now have a new ``src/Entity/Product.php`` file::
80117

81118
// src/Entity/Product.php
82119
namespace App\Entity;
@@ -95,96 +132,42 @@ You now have a new ``src/Entity/Product.php`` file::
95132
*/
96133
private $id;
97134

98-
// add your own fields
99-
}
100-
101-
This class is called an "entity". And soon, you will be able to save and query Product
102-
objects to a ``product`` table in your database.
103-
104-
.. _doctrine-adding-mapping:
105-
106-
Mapping More Fields / Columns
107-
-----------------------------
135+
/**
136+
* @ORM\Column(type="string", length=255)
137+
*/
138+
private $name;
108139

109-
Each property in the ``Product`` entity can be mapped to a column in the ``product``
110-
table. By adding some mapping configuration, Doctrine will be able to save a Product
111-
object to the ``product`` table *and* query from the ``product`` table and turn
112-
that data into ``Product`` objects:
140+
/**
141+
* @ORM\Column(type="integer")
142+
*/
143+
private $price;
113144

114-
.. image:: /_images/doctrine/mapping_single_entity.png
115-
:align: center
145+
public function getId()
146+
{
147+
return $this->id;
148+
}
116149

117-
Let's give the ``Product`` entity class three more properties and map them to columns
118-
in the database. This is usually done with annotations:
150+
// ... getter and setter methods
151+
}
119152

120-
.. configuration-block::
153+
.. note::
121154

122-
.. code-block:: php-annotations
155+
Confused why the price is an integer? Don't worry: this is just an example.
156+
But, storing prices as integers (e.g. 100 = $1 USD) can avoid rounding issues.
123157

124-
// src/Entity/Product.php
125-
// ...
158+
This class is called an "entity". And soon, you'll be able to save and query Product
159+
objects to a ``product`` table in your database. Each property in the ``Product``
160+
entity can be mapped to a column in that table. This is usually done with annotations:
161+
the ``@ORM\...`` comments that you see above each property:
126162

127-
// this use statement is needed for the annotations
128-
use Doctrine\ORM\Mapping as ORM;
163+
.. image:: /_images/doctrine/mapping_single_entity.png
164+
:align: center
129165

130-
class Product
131-
{
132-
/**
133-
* @ORM\Id
134-
* @ORM\GeneratedValue
135-
* @ORM\Column(type="integer")
136-
*/
137-
private $id;
138-
139-
/**
140-
* @ORM\Column(type="string", length=100)
141-
*/
142-
private $name;
143-
144-
/**
145-
* @ORM\Column(type="decimal", scale=2, nullable=true)
146-
*/
147-
private $price;
148-
}
166+
The ``make:entity`` command is a tool to make life easier. But this is *your* code:
167+
add/remove fields, add/remove methods or update configuration.
149168

150-
.. code-block:: yaml
151-
152-
# config/doctrine/Product.orm.yml
153-
App\Entity\Product:
154-
type: entity
155-
id:
156-
id:
157-
type: integer
158-
generator: { strategy: AUTO }
159-
fields:
160-
name:
161-
type: string
162-
length: 100
163-
price:
164-
type: decimal
165-
scale: 2
166-
nullable: true
167-
168-
.. code-block:: xml
169-
170-
<!-- config/doctrine/Product.orm.xml -->
171-
<?xml version="1.0" encoding="UTF-8" ?>
172-
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
173-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
174-
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
175-
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
176-
177-
<entity name="App\Entity\Product">
178-
<id name="id" type="integer">
179-
<generator strategy="AUTO" />
180-
</id>
181-
<field name="name" type="string" length="100" />
182-
<field name="price" type="decimal" scale="2" nullable="true" />
183-
</entity>
184-
</doctrine-mapping>
185-
186-
Doctrine supports a wide variety of different field types, each with their own options.
187-
To see a full list of types and options, see `Doctrine's Mapping Types documentation`_.
169+
Doctrine supports a wide variety of field types, each with their own options.
170+
To see a full list, check out `Doctrine's Mapping Types documentation`_.
188171
If you want to use XML instead of annotations, add ``type: xml`` and
189172
``dir: '%kernel.project_dir%/config/doctrine'`` to the entity mappings in your
190173
``config/packages/doctrine.yaml`` file.
@@ -193,7 +176,7 @@ If you want to use XML instead of annotations, add ``type: xml`` and
193176

194177
Be careful not to use reserved SQL keywords as your table or column names
195178
(e.g. ``GROUP`` or ``USER``). See Doctrine's `Reserved SQL keywords documentation`_
196-
for details on how to escape these. Or, configure the table name with
179+
for details on how to escape these. Or, change the table name with
197180
``@ORM\Table(name="groups")`` above the class or configure the column name with
198181
the ``name="group_name"`` option.
199182

@@ -204,17 +187,18 @@ Migrations: Creating the Database Tables/Schema
204187

205188
The ``Product`` class is fully-configured and ready to save to a ``product`` table.
206189
Of course, your database doesn't actually have the ``product`` table yet. To add
207-
the table, you can leverage the `DoctrineMigrationsBundle`_, which is already installed:
190+
it, you can leverage the `DoctrineMigrationsBundle`_, which is already installed:
208191

209192
.. code-block:: terminal
210193
211-
$ php bin/console doctrine:migrations:diff
194+
$ php bin/console make:migration
212195
213196
If everything worked, you should see something like this:
214197

215-
Generated new migration class to
216-
"/path/to/project/doctrine/src/Migrations/Version20171122151511.php"
217-
from schema differences.
198+
SUCCESS!
199+
200+
Next: Review the new migration "src/Migrations/Version20180207231217.php"
201+
Then: Run the migration with php bin/console doctrine:migrations:migrate
218202

219203
If you open this file, it contains the SQL needed to update your database! To run
220204
that SQL, execute your migrations:
@@ -224,12 +208,38 @@ that SQL, execute your migrations:
224208
$ php bin/console doctrine:migrations:migrate
225209
226210
This command executes all migration files that have not already been run against
227-
your database.
211+
your database. You should run this command on production when you deploy to keep
212+
your production database up-to-date.
228213

229214
Migrations & Adding more Fields
230215
-------------------------------
231216

232217
But what if you need to add a new field property to ``Product``, like a ``description``?
218+
It's easy to add the new property by hand. But, you can also use ``make:entity``
219+
again:
220+
221+
.. code-block:: terminal
222+
223+
$ php bin/console make:entity
224+
225+
Class name of the entity to create or update
226+
> Product
227+
228+
New property name (press <return> to stop adding fields):
229+
> description
230+
231+
Field type (enter ? to see all types) [string]:
232+
> text
233+
234+
Can this field be null in the database (nullable) (yes/no) [no]:
235+
> no
236+
237+
New property name (press <return> to stop adding fields):
238+
>
239+
(press enter again to finish)
240+
241+
This adds the new ``description`` property and ``getDescription()`` and ``setDescription()``
242+
methods:
233243

234244
.. code-block:: diff
235245
@@ -244,14 +254,16 @@ But what if you need to add a new field property to ``Product``, like a ``descri
244254
+ * @ORM\Column(type="text")
245255
+ */
246256
+ private $description;
257+
258+
// getDescription() & setDescription() were also added
247259
}
248260
249261
The new property is mapped, but it doesn't exist yet in the ``product`` table. No
250262
problem! Just generate a new migration:
251263

252264
.. code-block:: terminal
253265
254-
$ php bin/console doctrine:migrations:diff
266+
$ php bin/console make:migration
255267
256268
This time, the SQL in the generated file will look like this:
257269

@@ -269,55 +281,25 @@ before, execute your migrations:
269281
270282
This will only execute the *one* new migration file, because DoctrineMigrationsBundle
271283
knows that the first migration was already executed earlier. Behind the scenes, it
272-
automatically manages a ``migration_versions`` table to track this.
284+
manages a ``migration_versions`` table to track this.
273285

274286
Each time you make a change to your schema, run these two commands to generate the
275287
migration and then execute it. Be sure to commit the migration files and execute
276288
them when you deploy.
277289

278290
.. _doctrine-generating-getters-and-setters:
279291

280-
Generating Getters and Setters
281-
------------------------------
282-
283-
Doctrine now knows how to persist a ``Product`` object to the database. But the class
284-
itself isn't useful yet. All of the properties are ``private``, so there's no way
285-
to set data on them!
286-
287-
For that reason, you should create public getters and setters for all the fields
288-
you need to modify from outside of the class. If you use an IDE like PhpStorm, it
289-
can generate these for you. In PhpStorm, put your cursor anywhere in the class,
290-
then go to the Code -> Generate menu and select "Getters and Setters"::
291-
292-
// src/Entity/Product
293-
// ...
294-
295-
class Product
296-
{
297-
// all of your properties
298-
299-
public function getId()
300-
{
301-
return $this->id;
302-
}
303-
304-
public function getName()
305-
{
306-
return $this->name;
307-
}
292+
.. tip::
308293

309-
public function setName($name)
310-
{
311-
$this->name = $name;
312-
}
294+
If you prefer to add new properties manually, the ``make:entity`` command can
295+
generate the getter & setter methods for you:
313296

314-
// ... getters & setters for price & description
315-
}
297+
.. code-block:: terminal
316298
317-
.. tip::
299+
$ php bin/console make:entity --regenerate
318300
319-
Typically you won't need a ``setId()`` method: Doctrine will set the ID
320-
automatically.
301+
If you make some changes and want to regenerate *all* getter/setter methods,
302+
also pass ``--overwrite``.
321303

322304
Persisting Objects to the Database
323305
----------------------------------

0 commit comments

Comments
 (0)