23
23
24
24
.. code-block :: terminal
25
25
26
- composer require doctrine maker
26
+ composer require doctrine
27
+ composer require maker --dev
27
28
28
29
Configuring the Database
29
30
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -69,14 +70,50 @@ Creating an Entity Class
69
70
70
71
Suppose you're building an application where products need to be displayed.
71
72
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:
74
79
75
80
.. code-block :: terminal
76
81
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
78
101
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::
80
117
81
118
// src/Entity/Product.php
82
119
namespace App\Entity;
@@ -95,96 +132,42 @@ You now have a new ``src/Entity/Product.php`` file::
95
132
*/
96
133
private $id;
97
134
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;
108
139
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;
113
144
114
- .. image :: /_images/doctrine/mapping_single_entity.png
115
- :align: center
145
+ public function getId()
146
+ {
147
+ return $this->id;
148
+ }
116
149
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
+ }
119
152
120
- .. configuration-block ::
153
+ .. note ::
121
154
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.
123
157
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:
126
162
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
129
165
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.
149
168
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 `_.
188
171
If you want to use XML instead of annotations, add ``type: xml `` and
189
172
``dir: '%kernel.project_dir%/config/doctrine' `` to the entity mappings in your
190
173
``config/packages/doctrine.yaml `` file.
@@ -193,7 +176,7 @@ If you want to use XML instead of annotations, add ``type: xml`` and
193
176
194
177
Be careful not to use reserved SQL keywords as your table or column names
195
178
(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
197
180
``@ORM\Table(name="groups") `` above the class or configure the column name with
198
181
the ``name="group_name" `` option.
199
182
@@ -204,17 +187,18 @@ Migrations: Creating the Database Tables/Schema
204
187
205
188
The ``Product `` class is fully-configured and ready to save to a ``product `` table.
206
189
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:
208
191
209
192
.. code-block :: terminal
210
193
211
- $ php bin/console doctrine:migrations:diff
194
+ $ php bin/console make:migration
212
195
213
196
If everything worked, you should see something like this:
214
197
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
218
202
219
203
If you open this file, it contains the SQL needed to update your database! To run
220
204
that SQL, execute your migrations:
@@ -224,12 +208,38 @@ that SQL, execute your migrations:
224
208
$ php bin/console doctrine:migrations:migrate
225
209
226
210
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.
228
213
229
214
Migrations & Adding more Fields
230
215
-------------------------------
231
216
232
217
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:
233
243
234
244
.. code-block :: diff
235
245
@@ -244,14 +254,16 @@ But what if you need to add a new field property to ``Product``, like a ``descri
244
254
+ * @ORM\Column(type="text")
245
255
+ */
246
256
+ private $description;
257
+
258
+ // getDescription() & setDescription() were also added
247
259
}
248
260
249
261
The new property is mapped, but it doesn't exist yet in the ``product `` table. No
250
262
problem! Just generate a new migration:
251
263
252
264
.. code-block :: terminal
253
265
254
- $ php bin/console doctrine:migrations:diff
266
+ $ php bin/console make:migration
255
267
256
268
This time, the SQL in the generated file will look like this:
257
269
@@ -269,55 +281,25 @@ before, execute your migrations:
269
281
270
282
This will only execute the *one * new migration file, because DoctrineMigrationsBundle
271
283
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.
273
285
274
286
Each time you make a change to your schema, run these two commands to generate the
275
287
migration and then execute it. Be sure to commit the migration files and execute
276
288
them when you deploy.
277
289
278
290
.. _doctrine-generating-getters-and-setters :
279
291
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 ::
308
293
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:
313
296
314
- // ... getters & setters for price & description
315
- }
297
+ .. code-block :: terminal
316
298
317
- .. tip ::
299
+ $ php bin/console make:entity --regenerate
318
300
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 `` .
321
303
322
304
Persisting Objects to the Database
323
305
----------------------------------
0 commit comments