Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9190b73

Browse files
Merge pull request doctrine#7613 from SenseException/dql-select-subheadlines
Add headlines for direct references to DQL examples
2 parents df7e000 + 93d55d7 commit 9190b73

1 file changed

Lines changed: 68 additions & 26 deletions

File tree

docs/en/reference/dql-doctrine-query-language.rst

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -204,30 +204,37 @@ This section contains a large set of DQL queries and some
204204
explanations of what is happening. The actual result also depends
205205
on the hydration mode.
206206

207-
Hydrate all User entities:
207+
Hydrate all User entities
208+
^^^^^^^^^^^^^^^^^^^^^^^^^
208209

209210
.. code-block:: php
210211
211212
<?php
212213
$query = $em->createQuery('SELECT u FROM MyProject\Model\User u');
213214
$users = $query->getResult(); // array of User objects
214215
215-
Retrieve the IDs of all CmsUsers:
216+
Retrieve the IDs of all CmsUsers
217+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
216218

217219
.. code-block:: php
218220
219221
<?php
220222
$query = $em->createQuery('SELECT u.id FROM CmsUser u');
221223
$ids = $query->getResult(); // array of CmsUser ids
222224
223-
Retrieve the IDs of all users that have written an article:
225+
Retrieve the IDs of all users that have written an article
226+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
224227

225228
.. code-block:: php
226229
227230
<?php
228231
$query = $em->createQuery('SELECT DISTINCT u.id FROM CmsArticle a JOIN a.user u');
229232
$ids = $query->getResult(); // array of CmsUser ids
230233
234+
235+
Retrieve all articles and sort them by username
236+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
237+
231238
Retrieve all articles and sort them by the name of the articles
232239
users instance:
233240

@@ -237,7 +244,8 @@ users instance:
237244
$query = $em->createQuery('SELECT a FROM CmsArticle a JOIN a.user u ORDER BY u.name ASC');
238245
$articles = $query->getResult(); // array of CmsArticle objects
239246
240-
Retrieve the Username and Name of a CmsUser:
247+
Retrieve the Username and Name of a CmsUser
248+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
241249

242250
.. code-block:: php
243251
@@ -246,7 +254,8 @@ Retrieve the Username and Name of a CmsUser:
246254
$users = $query->getResult(); // array of CmsUser username and name values
247255
echo $users[0]['username'];
248256
249-
Retrieve a ForumUser and his single associated entity:
257+
Retrieve a ForumUser and his single associated entity
258+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
250259

251260
.. code-block:: php
252261
@@ -255,7 +264,8 @@ Retrieve a ForumUser and his single associated entity:
255264
$users = $query->getResult(); // array of ForumUser objects with the avatar association loaded
256265
echo get_class($users[0]->getAvatar());
257266
258-
Retrieve a CmsUser and fetch join all the phonenumbers he has:
267+
Retrieve a CmsUser and fetch join all owning phonenumbers
268+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
259269

260270
.. code-block:: php
261271
@@ -264,23 +274,26 @@ Retrieve a CmsUser and fetch join all the phonenumbers he has:
264274
$users = $query->getResult(); // array of CmsUser objects with the phonenumbers association loaded
265275
$phonenumbers = $users[0]->getPhonenumbers();
266276
267-
Hydrate a result in Ascending:
277+
Hydrate a result in Ascending
278+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
268279

269280
.. code-block:: php
270281
271282
<?php
272283
$query = $em->createQuery('SELECT u FROM ForumUser u ORDER BY u.id ASC');
273284
$users = $query->getResult(); // array of ForumUser objects
274285
275-
Or in Descending Order:
286+
Hydrate a result in Descending Order
287+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
276288

277289
.. code-block:: php
278290
279291
<?php
280292
$query = $em->createQuery('SELECT u FROM ForumUser u ORDER BY u.id DESC');
281293
$users = $query->getResult(); // array of ForumUser objects
282294
283-
Using Aggregate Functions:
295+
Using Aggregate Functions
296+
^^^^^^^^^^^^^^^^^^^^^^^^^
284297

285298
.. code-block:: php
286299
@@ -291,7 +304,8 @@ Using Aggregate Functions:
291304
$query = $em->createQuery('SELECT u, count(g.id) FROM Entities\User u JOIN u.groups g GROUP BY u.id');
292305
$result = $query->getResult();
293306
294-
With WHERE Clause and Positional Parameter:
307+
Using WHERE Clause and Positional Parameter
308+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
295309

296310
.. code-block:: php
297311
@@ -300,7 +314,8 @@ With WHERE Clause and Positional Parameter:
300314
$query->setParameter(1, 321);
301315
$users = $query->getResult(); // array of ForumUser objects
302316
303-
With WHERE Clause and Named Parameter:
317+
Using WHERE Clause and Named Parameter
318+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304319

305320
.. code-block:: php
306321
@@ -309,7 +324,8 @@ With WHERE Clause and Named Parameter:
309324
$query->setParameter('name', 'Bob');
310325
$users = $query->getResult(); // array of ForumUser objects
311326
312-
With Nested Conditions in WHERE Clause:
327+
Using Nested Conditions in WHERE Clause
328+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
313329

314330
.. code-block:: php
315331
@@ -322,22 +338,27 @@ With Nested Conditions in WHERE Clause:
322338
));
323339
$users = $query->getResult(); // array of ForumUser objects
324340
325-
With COUNT DISTINCT:
341+
COUNT DISTINCT
342+
^^^^^^^^^^^^^^
326343

327344
.. code-block:: php
328345
329346
<?php
330347
$query = $em->createQuery('SELECT COUNT(DISTINCT u.name) FROM CmsUser');
331348
$users = $query->getResult(); // array of ForumUser objects
332349
333-
With Arithmetic Expression in WHERE clause:
350+
Using Arithmetic Expression in WHERE clause
351+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
334352

335353
.. code-block:: php
336354
337355
<?php
338356
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000');
339357
$users = $query->getResult(); // array of ForumUser objects
340358
359+
Hide aliased columns from the result
360+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
361+
341362
Retrieve user entities with Arithmetic Expression in ORDER clause, using the ``HIDDEN`` keyword:
342363

343364
.. code-block:: php
@@ -346,6 +367,9 @@ Retrieve user entities with Arithmetic Expression in ORDER clause, using the ``H
346367
$query = $em->createQuery('SELECT u, u.posts_count + u.likes_count AS HIDDEN score FROM CmsUser u ORDER BY score');
347368
$users = $query->getResult(); // array of User objects
348369
370+
Select all user-ids and optionally associated article-ids
371+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
372+
349373
Using a LEFT JOIN to hydrate all user-ids and optionally associated
350374
article-ids:
351375

@@ -355,8 +379,8 @@ article-ids:
355379
$query = $em->createQuery('SELECT u.id, a.id as article_id FROM CmsUser u LEFT JOIN u.articles a');
356380
$results = $query->getResult(); // array of user ids and every article_id for each user
357381
358-
Restricting a JOIN clause by additional conditions specified by
359-
WITH:
382+
Restricting a JOIN clause by additional conditions specified by WITH
383+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
360384

361385
.. code-block:: php
362386
@@ -365,15 +389,17 @@ WITH:
365389
$query->setParameter('foo', '%foo%');
366390
$users = $query->getResult();
367391
368-
Using several Fetch JOINs:
392+
Using several Fetch JOINs
393+
^^^^^^^^^^^^^^^^^^^^^^^^^
369394

370395
.. code-block:: php
371396
372397
<?php
373398
$query = $em->createQuery('SELECT u, a, p, c FROM CmsUser u JOIN u.articles a JOIN u.phonenumbers p JOIN a.comments c');
374399
$users = $query->getResult();
375400
376-
BETWEEN in WHERE clause:
401+
BETWEEN in WHERE clause
402+
^^^^^^^^^^^^^^^^^^^^^^^
377403

378404
.. code-block:: php
379405
@@ -383,15 +409,17 @@ BETWEEN in WHERE clause:
383409
$query->setParameter(2, 321);
384410
$usernames = $query->getResult();
385411
386-
DQL Functions in WHERE clause:
412+
DQL Functions in WHERE clause
413+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
387414

388415
.. code-block:: php
389416
390417
<?php
391418
$query = $em->createQuery("SELECT u.name FROM CmsUser u WHERE TRIM(u.name) = 'someone'");
392419
$usernames = $query->getResult();
393420
394-
IN() Expression:
421+
IN() Expression
422+
^^^^^^^^^^^^^^^
395423

396424
.. code-block:: php
397425
@@ -405,7 +433,8 @@ IN() Expression:
405433
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE u.id NOT IN (1)');
406434
$users = $query->getResult();
407435
408-
CONCAT() DQL Function:
436+
CONCAT() DQL Function
437+
^^^^^^^^^^^^^^^^^^^^^
409438

410439
.. code-block:: php
411440
@@ -419,14 +448,16 @@ CONCAT() DQL Function:
419448
$idUsernames = $query->getResult();
420449
421450
EXISTS in WHERE clause with correlated Subquery
451+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
422452

423453
.. code-block:: php
424454
425455
<?php
426456
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');
427457
$ids = $query->getResult();
428458
429-
Get all users who are members of $group.
459+
Get all users who are members of $group
460+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
430461

431462
.. code-block:: php
432463
@@ -436,6 +467,7 @@ Get all users who are members of $group.
436467
$ids = $query->getResult();
437468
438469
Get all users that have more than 1 phonenumber
470+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
439471

440472
.. code-block:: php
441473
@@ -444,15 +476,19 @@ Get all users that have more than 1 phonenumber
444476
$users = $query->getResult();
445477
446478
Get all users that have no phonenumber
479+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
447480

448481
.. code-block:: php
449482
450483
<?php
451484
$query = $em->createQuery('SELECT u FROM CmsUser u WHERE u.phonenumbers IS EMPTY');
452485
$users = $query->getResult();
453486
454-
Get all instances of a specific type, for use with inheritance
455-
hierarchies:
487+
Get all instances of a specific type
488+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
489+
490+
Get all instances of a specific type, for use with inheritance hierarchies. These queries can be useful for
491+
:doc:`inheritance mapping <inheritance-mapping>`.
456492

457493
.. versionadded:: 2.1
458494

@@ -463,7 +499,10 @@ hierarchies:
463499
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1');
464500
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u NOT INSTANCE OF ?1');
465501
466-
Get all users visible on a given website that have chosen certain gender:
502+
Using IDENTITY() in queries
503+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
504+
505+
Get all users visible on a given website that have chosen certain gender.
467506

468507
.. versionadded:: 2.2
469508

@@ -474,13 +513,16 @@ Get all users visible on a given website that have chosen certain gender:
474513
475514
.. versionadded:: 2.4
476515

477-
Starting with 2.4, the IDENTITY() DQL function also works for composite primary keys:
516+
Starting with 2.4, the IDENTITY() DQL function also works for composite primary keys
478517

479518
.. code-block:: php
480519
481520
<?php
482521
$query = $em->createQuery("SELECT IDENTITY(c.location, 'latitude') AS latitude, IDENTITY(c.location, 'longitude') AS longitude FROM Checkpoint c WHERE c.user = ?1");
483522
523+
Arbitrary Join
524+
^^^^^^^^^^^^^^
525+
484526
Joins between entities without associations were not possible until version
485527
2.4, where you can generate an arbitrary join with the following syntax:
486528

0 commit comments

Comments
 (0)