@@ -47,7 +47,7 @@ any PHP class:
47
47
48
48
.. code-block :: php
49
49
50
- // Acme/HelloBundle/Entity/User.php
50
+ // src/ Acme/HelloBundle/Entity/User.php
51
51
namespace Acme\HelloBundle\Entity;
52
52
53
53
class User
@@ -85,7 +85,7 @@ write mapping information with annotations, XML, or YAML:
85
85
86
86
.. code-block :: php-annotations
87
87
88
- // Acme/HelloBundle/Entity/User.php
88
+ // src/ Acme/HelloBundle/Entity/User.php
89
89
namespace Acme\HelloBundle\Entity;
90
90
91
91
/**
@@ -108,7 +108,7 @@ write mapping information with annotations, XML, or YAML:
108
108
109
109
.. code-block :: yaml
110
110
111
- # Acme/HelloBundle/Resources/config/doctrine/Acme.HelloBundle.Entity.User.orm.yml
111
+ # src/ Acme/HelloBundle/Resources/config/doctrine/Acme.HelloBundle.Entity.User.orm.yml
112
112
Acme\HelloBundle\Entity\User :
113
113
type : entity
114
114
table : user
@@ -124,7 +124,7 @@ write mapping information with annotations, XML, or YAML:
124
124
125
125
.. code-block :: xml
126
126
127
- <!-- Acme/HelloBundle/Resources/config/doctrine/Acme.HelloBundle.Entity.User.orm.xml -->
127
+ <!-- src/ Acme/HelloBundle/Resources/config/doctrine/Acme.HelloBundle.Entity.User.orm.xml -->
128
128
<doctrine-mapping xmlns =" http://doctrine-project.org/schemas/orm/doctrine-mapping"
129
129
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
130
130
xsi : schemaLocation =" http://doctrine-project.org/schemas/orm/doctrine-mapping
@@ -167,7 +167,7 @@ Eventually, use your entity and manage its persistent state with Doctrine:
167
167
168
168
.. code-block :: php
169
169
170
- // Acme/HelloBundle/Controller/UserController.php
170
+ // src/ Acme/HelloBundle/Controller/UserController.php
171
171
namespace Acme\HelloBundle\Controller;
172
172
173
173
use Acme\HelloBundle\Entity\User;
@@ -215,6 +215,7 @@ losing your existing data. So first let's just add a new property to our
215
215
216
216
.. code-block :: php
217
217
218
+ // src/Acme/HelloBundle/Entity/User.php
218
219
namespace Acme\HelloBundle\Entity;
219
220
220
221
/** @orm:Entity */
@@ -234,6 +235,166 @@ you just need to run the following command:
234
235
Now your database will be updated and the new column added to the database
235
236
table.
236
237
238
+ .. index ::
239
+ single: Doctrine ORM; Queries;
240
+
241
+ Queries
242
+ ~~~~~~~
243
+
244
+ As you have already seen, working with single objects is straightforward and
245
+ easy with the entity manager. But how can you query for a set of objects?
246
+ As every with any Doctrine operation, this is done via the entity manager.
247
+ Change the delete action used in the previous example to use a query instead
248
+ of loading the object and then deleting it afterwards:
249
+
250
+ .. code-block :: php
251
+
252
+ public function deleteAction($id)
253
+ {
254
+ $query = $this->get('doctrine')->getEntityManager()
255
+ ->createQuery('DELETE FROM Acme\HelloBundle\Entity\User u
256
+ WHERE u.id = :id');
257
+ $query->setParameters(array(
258
+ 'id' => $id
259
+ ));
260
+
261
+ $query->execute();
262
+
263
+ // ...
264
+ }
265
+
266
+ Of course you can use SELECT and UPDATE queries too. Doctrine brings its own
267
+ Query Language called DQL (Doctrine Query Language). The DQL has some
268
+ similarities with SQL but is a query language with its own syntax.
269
+
270
+ .. tip ::
271
+
272
+ You can read more about the Doctrine Query Language on the official
273
+ `Doctrine Query Language documentation `_ website. The advantage of DQL
274
+ is that it is database-agnostic - the same queries can be written in
275
+ DQL and work with any supported database engine.
276
+
277
+ .. note ::
278
+
279
+ There is a drawback when using a DQL DELETE or UPDATE statement. Specifically,
280
+ since this operation is made directly to the database, Doctrine isn't
281
+ aware of the option internally. For example, if you query for an object
282
+ and then make an update to that object directly in the database via an
283
+ UPDATE statement, the object itself won't reflect that update. So, be
284
+ careful when using these statements - your objects can become out-of-sync
285
+ with your database inside a single request.
286
+
287
+ .. index ::
288
+ single: Doctrine ORM; Repository;
289
+
290
+ Repositories
291
+ ~~~~~~~~~~~~
292
+
293
+ It is bad practice to make queries in the Symfony controllers. Such queries
294
+ should be done in the model layer of your bundle so that they can be tested
295
+ and reused through your application. Fortunately, Doctrine allows you to
296
+ use special classes called Repositories to encapsulate queries.
297
+
298
+ Doctrine provides a default implementation for your repository classes, so you
299
+ can use their common methods to query your entities data. One of them is the
300
+ ``findAll `` function.
301
+
302
+ .. code-block :: php
303
+
304
+ $em = $this->get('doctrine')->getEntityManager();
305
+ $users = $em->getRepository('AcmeHelloBundle:User')->findAll();
306
+
307
+ If you want to create your own function to query or manipulate your data, you
308
+ need to create a custom repository class for an entity. To do so, you need to
309
+ add the name of the repository class to your mapping definition.
310
+
311
+ .. configuration-block ::
312
+
313
+ .. code-block :: php-annotations
314
+
315
+ // src/Acme/HelloBundle/Entity/User.php
316
+ namespace Acme\HelloBundle\Entity;
317
+
318
+ /**
319
+ * @orm:Entity(repositoryClass="Acme\HelloBundle\Repository\UserRepository")
320
+ */
321
+ class User
322
+ {
323
+ //...
324
+ }
325
+
326
+ .. code-block :: yaml
327
+
328
+ # src/Acme/HelloBundle/Resources/config/doctrine/Acme.HelloBundle.Entity.User.orm.yml
329
+ Acme\HelloBundle\Entity\User :
330
+ type : entity
331
+ table : user
332
+ repositoryClass : Acme\HelloBundle\Repository\UserRepository
333
+ # ...
334
+
335
+ .. code-block :: xml
336
+
337
+ <!-- src/Acme/HelloBundle/Resources/config/doctrine/Acme.HelloBundle.Entity.User.orm.xml -->
338
+ <doctrine-mapping xmlns =" http://doctrine-project.org/schemas/orm/doctrine-mapping"
339
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
340
+ xsi : schemaLocation =" http://doctrine-project.org/schemas/orm/doctrine-mapping
341
+ http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd" >
342
+
343
+ <entity name =" Acme\HelloBundle\Entity\User" table =" user"
344
+ repository-class =" Acme\HelloBundle\Repository\UserRepository" >
345
+ <id name =" id" type =" integer" column =" id" >
346
+ <generator strategy =" AUTO" />
347
+ </id >
348
+ <field name =" name" column =" name" type =" string" length =" 255" />
349
+ </entity >
350
+
351
+ </doctrine-mapping >
352
+
353
+ The repository class is created for you if you run the following command
354
+ to generate your entities.
355
+
356
+ $ php app/console doctrine:generate: entities
357
+
358
+ If you have already generated your entity class before adding the ``repositoryClass ``
359
+ mapping, you have to create the class on your own. Fortunately, it's pretty
360
+ easy. Simply create the class in the ``Repository `` directory of your bundle
361
+ and be sure it extends ``Doctrine\ORM\EntityRepository ``. Once you've created
362
+ the class, you can add any method to query your entities.
363
+
364
+ The following code shows a sample repository class.
365
+
366
+ .. code-block :: php
367
+
368
+ // src/Acme/HelloBundle/Repository/UserRepository.php
369
+ namespace Acme\HelloBundle\Repository;
370
+
371
+ use Doctrine\ORM\EntityRepository;
372
+
373
+ class UserRepository extends EntityRepository
374
+ {
375
+ public function findAllOrderedByName()
376
+ {
377
+ return $this->getEntityManager()
378
+ ->createQuery('SELECT u FROM Acme\HelloBundle\Entity\User u
379
+ ORDER BY u.name ASC')
380
+ ->getResult();
381
+ }
382
+ }
383
+
384
+ .. tip ::
385
+
386
+ The entity manager can be accessed via ``$this->getEntityManager() `` in the
387
+ repositories functions.
388
+
389
+ The usage of this new method is the same as with the default finder functions.
390
+
391
+ .. code-block :: php
392
+
393
+ $em = $this->get('doctrine')->getEntityManager();
394
+ $users = $em->getRepository('AcmeHelloBundle:User')
395
+ ->findAllOrderedByName();
396
+
397
+
237
398
.. index ::
238
399
single: Configuration; Doctrine ORM
239
400
single: Doctrine; ORM Configuration
@@ -242,7 +403,7 @@ Configuration
242
403
-------------
243
404
244
405
In the overview we already described the only necessary configuration option
245
- to get the Doctrine ORM running with Symfony 2 . All the other configuration
406
+ to get the Doctrine ORM running with Symfony2 . All the other configuration
246
407
options are used with reasonable default values.
247
408
248
409
This following configuration example shows all the configuration defaults that
@@ -356,7 +517,7 @@ The following configuration shows a bunch of mapping examples:
356
517
Multiple Entity Managers
357
518
~~~~~~~~~~~~~~~~~~~~~~~~
358
519
359
- You can use multiple `` EntityManager``s in a Symfony2 application. This is
520
+ You can use multiple entity managers in a Symfony2 application. This is
360
521
necessary if you are using different databases or even vendors with entirely
361
522
different sets of entities.
362
523
@@ -617,3 +778,4 @@ get the choices. If not set all entities will be used.
617
778
618
779
.. _documentation : http://www.doctrine-project.org/docs/orm/2.0/en
619
780
.. _Doctrine : http://www.doctrine-project.org
781
+ .. _Doctrine Query Language documentation : http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html
0 commit comments