/usr/share/doc/php-doctrine-orm/html/reference/dql-doctrine-query-language.html is in doctrine-orm-doc 2.4.6-1+deb8u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>15. Doctrine Query Language — Doctrine 2 ORM 2 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/layout.css" type="text/css" />
<link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script src="../_static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
$("#versions").change(function() {
var docsUrl = $(this).val();
window.location.href = docsUrl;
});
});
-->
</script>
<link rel="shortcut icon" href="../_static/doctrine.ico"/>
<link rel="top" title="Doctrine 2 ORM 2 documentation" href="../index.html" />
<link rel="next" title="16. The QueryBuilder" href="query-builder.html" />
<link rel="prev" title="14. Batch Processing" href="batch-processing.html" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1 id="h1title"></h1>
<div id="logo">
<a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
</div>
</div>
<div id="nav" class="cls">
<div class="tl cls">
<ul>
<li><a target="_top" href="/">home</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/about">about</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/projects.html">projects</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/projects/orm">orm</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/projects/dbal">dbal</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/blog">blog</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/jira">development</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/contribute">contribute</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/community">community</a></li>
</ul>
</div>
</div>
<div id="content" class="cls">
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="query-builder.html" title="16. The QueryBuilder"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="batch-processing.html" title="14. Batch Processing"
accesskey="P">previous</a> |</li>
<li><a href="/">Doctrine Homepage</a> »</li>
<li><a href="../index.html">Doctrine 2 ORM 2 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" >
<div class="section" id="doctrine-query-language">
<h1>15. Doctrine Query Language<a class="headerlink" href="#doctrine-query-language" title="Permalink to this headline">¶</a></h1>
<p>DQL stands for Doctrine Query Language and is an Object
Query Language derivate that is very similar to the Hibernate
Query Language (HQL) or the Java Persistence Query Language (JPQL).</p>
<p>In essence, DQL provides powerful querying capabilities over your
object model. Imagine all your objects lying around in some storage
(like an object database). When writing DQL queries, think about
querying that storage to pick a certain subset of your objects.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">A common mistake for beginners is to mistake DQL for
being just some form of SQL and therefore trying to use table names
and column names or join arbitrary tables together in a query. You
need to think about DQL as a query language for your object model,
not for your relational schema.</p>
</div>
<p>DQL is case in-sensitive, except for namespace, class and field
names, which are case sensitive.</p>
<div class="section" id="types-of-dql-queries">
<h2>15.1. Types of DQL queries<a class="headerlink" href="#types-of-dql-queries" title="Permalink to this headline">¶</a></h2>
<p>DQL as a query language has SELECT, UPDATE and DELETE constructs
that map to their corresponding SQL statement types. INSERT
statements are not allowed in DQL, because entities and their
relations have to be introduced into the persistence context
through <tt class="docutils literal"><span class="pre">EntityManager#persist()</span></tt> to ensure consistency of your
object model.</p>
<p>DQL SELECT statements are a very powerful way of retrieving parts
of your domain model that are not accessible via associations.
Additionally they allow to retrieve entities and their associations
in one single SQL select statement which can make a huge difference
in performance in contrast to using several queries.</p>
<p>DQL UPDATE and DELETE statements offer a way to execute bulk
changes on the entities of your domain model. This is often
necessary when you cannot load all the affected entities of a bulk
update into memory.</p>
</div>
<div class="section" id="select-queries">
<h2>15.2. SELECT queries<a class="headerlink" href="#select-queries" title="Permalink to this headline">¶</a></h2>
<div class="section" id="dql-select-clause">
<h3>15.2.1. DQL SELECT clause<a class="headerlink" href="#dql-select-clause" title="Permalink to this headline">¶</a></h3>
<p>The select clause of a DQL query specifies what appears in the
query result. The composition of all the expressions in the select
clause also influences the nature of the query result.</p>
<p>Here is an example that selects all users with an age > 20:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM MyProject\Model\User u WHERE u.age > 20'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Lets examine the query:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">u</span></tt> is a so called identification variable or alias that
refers to the <tt class="docutils literal"><span class="pre">MyProject\Model\User</span></tt> class. By placing this alias
in the SELECT clause we specify that we want all instances of the
User class that are matched by this query to appear in the query
result.</li>
<li>The FROM keyword is always followed by a fully-qualified class
name which in turn is followed by an identification variable or
alias for that class name. This class designates a root of our
query from which we can navigate further via joins (explained
later) and path expressions.</li>
<li>The expression <tt class="docutils literal"><span class="pre">u.age</span></tt> in the WHERE clause is a path
expression. Path expressions in DQL are easily identified by the
use of the ‘.’ operator that is used for constructing paths. The
path expression <tt class="docutils literal"><span class="pre">u.age</span></tt> refers to the <tt class="docutils literal"><span class="pre">age</span></tt> field on the User
class.</li>
</ul>
<p>The result of this query would be a list of User objects where all
users are older than 20.</p>
<p>The SELECT clause allows to specify both class identification
variables that signal the hydration of a complete entity class or
just fields of the entity using the syntax <tt class="docutils literal"><span class="pre">u.name</span></tt>. Combinations
of both are also allowed and it is possible to wrap both fields and
identification values into aggregation and DQL functions. Numerical
fields can be part of computations using mathematical operations.
See the sub-section on <a class="reference internal" href="#functions-operators-aggregates">Functions, Operators, Aggregates</a> for
more information.</p>
</div>
<div class="section" id="joins">
<h3>15.2.2. Joins<a class="headerlink" href="#joins" title="Permalink to this headline">¶</a></h3>
<p>A SELECT query can contain joins. There are 2 types of JOINs:
“Regular” Joins and “Fetch” Joins.</p>
<p><strong>Regular Joins</strong>: Used to limit the results and/or compute
aggregate values.</p>
<p><strong>Fetch Joins</strong>: In addition to the uses of regular joins: Used to
fetch related entities and include them in the hydrated result of a
query.</p>
<p>There is no special DQL keyword that distinguishes a regular join
from a fetch join. A join (be it an inner or outer join) becomes a
“fetch join” as soon as fields of the joined entity appear in the
SELECT part of the DQL query outside of an aggregate function.
Otherwise its a “regular join”.</p>
<p>Example:</p>
<p>Regular join of the address:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s2">"SELECT u FROM User u JOIN u.address a WHERE a.city = 'Berlin'"</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Fetch join of the address:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s2">"SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'"</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>When Doctrine hydrates a query with fetch-join it returns the class
in the FROM clause on the root level of the result array. In the
previous example an array of User instances is returned and the
address of each user is fetched and hydrated into the
<tt class="docutils literal"><span class="pre">User#address</span></tt> variable. If you access the address Doctrine does
not need to lazy load the association with another query.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Doctrine allows you to walk all the associations between
all the objects in your domain model. Objects that were not already
loaded from the database are replaced with lazy load proxy
instances. Non-loaded Collections are also replaced by lazy-load
instances that fetch all the contained objects upon first access.
However relying on the lazy-load mechanism leads to many small
queries executed against the database, which can significantly
affect the performance of your application. <strong>Fetch Joins</strong> are the
solution to hydrate most or all of the entities that you need in a
single SELECT query.</p>
</div>
</div>
<div class="section" id="named-and-positional-parameters">
<h3>15.2.3. Named and Positional Parameters<a class="headerlink" href="#named-and-positional-parameters" title="Permalink to this headline">¶</a></h3>
<p>DQL supports both named and positional parameters, however in
contrast to many SQL dialects positional parameters are specified
with numbers, for example ”?1”, ”?2” and so on. Named parameters
are specified with ”:name1”, ”:name2” and so on.</p>
<p>When referencing the parameters in <tt class="docutils literal"><span class="pre">Query#setParameter($param,</span> <span class="pre">$value)</span></tt>
both named and positional parameters are used <strong>without</strong> their prefixes.</p>
</div>
<div class="section" id="dql-select-examples">
<h3>15.2.4. DQL SELECT Examples<a class="headerlink" href="#dql-select-examples" title="Permalink to this headline">¶</a></h3>
<p>This section contains a large set of DQL queries and some
explanations of what is happening. The actual result also depends
on the hydration mode.</p>
<p>Hydrate all User entities:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM MyProject\Model\User u'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of User objects</span>
</pre></div>
</div>
<p>Retrieve the IDs of all CmsUsers:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.id FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$ids</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CmsUser ids</span>
</pre></div>
</div>
<p>Retrieve the IDs of all users that have written an article:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT DISTINCT u.id FROM CmsArticle a JOIN a.user u'</span><span class="p">);</span>
<span class="nv">$ids</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CmsUser ids</span>
</pre></div>
</div>
<p>Retrieve all articles and sort them by the name of the articles
users instance:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT a FROM CmsArticle a JOIN a.user u ORDER BY u.name ASC'</span><span class="p">);</span>
<span class="nv">$articles</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CmsArticle objects</span>
</pre></div>
</div>
<p>Retrieve the Username and Name of a CmsUser:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.username, u.name FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CmsUser username and name values</span>
<span class="k">echo</span> <span class="nv">$users</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">'username'</span><span class="p">];</span>
</pre></div>
</div>
<p>Retrieve a ForumUser and his single associated entity:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u, a FROM ForumUser u JOIN u.avatar a'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects with the avatar association loaded</span>
<span class="k">echo</span> <span class="nb">get_class</span><span class="p">(</span><span class="nv">$users</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-></span><span class="na">getAvatar</span><span class="p">());</span>
</pre></div>
</div>
<p>Retrieve a CmsUser and fetch join all the phonenumbers he has:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u, p FROM CmsUser u JOIN u.phonenumbers p'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CmsUser objects with the phonenumbers association loaded</span>
<span class="nv">$phonenumbers</span> <span class="o">=</span> <span class="nv">$users</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-></span><span class="na">getPhonenumbers</span><span class="p">();</span>
</pre></div>
</div>
<p>Hydrate a result in Ascending:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM ForumUser u ORDER BY u.id ASC'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>Or in Descending Order:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM ForumUser u ORDER BY u.id DESC'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>Using Aggregate Functions:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT COUNT(u.id) FROM Entities\User u'</span><span class="p">);</span>
<span class="nv">$count</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getSingleScalarResult</span><span class="p">();</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u, count(g.id) FROM Entities\User u JOIN u.groups g GROUP BY u.id'</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>With WHERE Clause and Positional Parameter:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM ForumUser u WHERE u.id = ?1'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">321</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>With WHERE Clause and Named Parameter:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM ForumUser u WHERE u.username = :name'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'Bob'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>With Nested Conditions in WHERE Clause:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u from ForumUser u WHERE (u.username = :name OR u.username = :name2) AND u.id = :id'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameters</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'name'</span> <span class="o">=></span> <span class="s1">'Bob'</span><span class="p">,</span>
<span class="s1">'name2'</span> <span class="o">=></span> <span class="s1">'Alice'</span><span class="p">,</span>
<span class="s1">'id'</span> <span class="o">=></span> <span class="mi">321</span><span class="p">,</span>
<span class="p">));</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>With COUNT DISTINCT:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT COUNT(DISTINCT u.name) FROM CmsUser'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>With Arithmetic Expression in WHERE clause:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of ForumUser objects</span>
</pre></div>
</div>
<p>Using a LEFT JOIN to hydrate all user-ids and optionally associated
article-ids:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.id, a.id as article_id FROM CmsUser u LEFT JOIN u.articles a'</span><span class="p">);</span>
<span class="nv">$results</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of user ids and every article_id for each user</span>
</pre></div>
</div>
<p>Restricting a JOIN clause by additional conditions:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s2">"SELECT u FROM CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE :foo"</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'%foo%'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Using several Fetch JOINs:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u, a, p, c FROM CmsUser u JOIN u.articles a JOIN u.phonenumbers p JOIN a.comments c'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>BETWEEN in WHERE clause:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.name FROM CmsUser u WHERE u.id BETWEEN ?1 AND ?2'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">321</span><span class="p">);</span>
<span class="nv">$usernames</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>DQL Functions in WHERE clause:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s2">"SELECT u.name FROM CmsUser u WHERE TRIM(u.name) = 'someone'"</span><span class="p">);</span>
<span class="nv">$usernames</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>IN() Expression:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.name FROM CmsUser u WHERE u.id IN(46)'</span><span class="p">);</span>
<span class="nv">$usernames</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u WHERE u.id IN (1, 2)'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u WHERE u.id NOT IN (1)'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>CONCAT() DQL Function:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s2">"SELECT u.id FROM CmsUser u WHERE CONCAT(u.name, 's') = ?1"</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'Jess'</span><span class="p">);</span>
<span class="nv">$ids</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT CONCAT(u.id, u.name) FROM CmsUser u WHERE u.id = ?1'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">321</span><span class="p">);</span>
<span class="nv">$idUsernames</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>EXISTS in WHERE clause with correlated Subquery</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)'</span><span class="p">);</span>
<span class="nv">$ids</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Get all users who are members of $group.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u.id FROM CmsUser u WHERE :groupId MEMBER OF u.groups'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="s1">'groupId'</span><span class="p">,</span> <span class="nv">$group</span><span class="p">);</span>
<span class="nv">$ids</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Get all users that have more than 1 phonenumber</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u WHERE SIZE(u.phonenumbers) > 1'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Get all users that have no phonenumber</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u WHERE u.phonenumbers IS EMPTY'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span>
</pre></div>
</div>
<p>Get all instances of a specific type, for use with inheritance
hierarchies:</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee'</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1'</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u NOT INSTANCE OF ?1'</span><span class="p">);</span>
</pre></div>
</div>
<p>Get all users visible on a given website that have chosen certain gender:</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.2.</span></p>
</div>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM User u WHERE u.gender IN (SELECT IDENTITY(agl.gender) FROM Site s JOIN s.activeGenderList agl WHERE s.id = ?1)'</span><span class="p">);</span>
</pre></div>
</div>
<p>Starting with 2.4, the IDENTITY() DQL function also works for composite primary keys:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT IDENTITY(c.location, '</span><span class="nx">latitude</span><span class="s1">') AS latitude, IDENTITY(c.location, '</span><span class="nx">longitude</span><span class="s1">') AS longitude FROM Checkpoint c WHERE c.user = ?1'</span><span class="p">);</span>
</pre></div>
</div>
<div class="section" id="partial-object-syntax">
<h4>15.2.4.1. Partial Object Syntax<a class="headerlink" href="#partial-object-syntax" title="Permalink to this headline">¶</a></h4>
<p>By default when you run a DQL query in Doctrine and select only a
subset of the fields for a given entity, you do not receive objects
back. Instead, you receive only arrays as a flat rectangular result
set, similar to how you would if you were just using SQL directly
and joining some data.</p>
<p>If you want to select partial objects you can use the <tt class="docutils literal"><span class="pre">partial</span></tt>
DQL keyword:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT partial u.{id, username} FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of partially loaded CmsUser objects</span>
</pre></div>
</div>
<p>You use the partial syntax when joining as well:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT partial u.{id, username}, partial a.{id, name} FROM CmsUser u JOIN u.articles a'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of partially loaded CmsUser objects</span>
</pre></div>
</div>
</div>
<div class="section" id="new-operator-syntax">
<h4>15.2.4.2. “NEW” Operator Syntax<a class="headerlink" href="#new-operator-syntax" title="Permalink to this headline">¶</a></h4>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
<p>Using the <tt class="docutils literal"><span class="pre">NEW</span></tt> operator you can construct Data Transfer Objects (DTOs) directly from DQL queries.</p>
<ul class="simple">
<li>When using <tt class="docutils literal"><span class="pre">SELECT</span> <span class="pre">NEW</span></tt> you don’t need to specify a mapped entity.</li>
<li>You can specify any PHP class, it’s only require that the constructor of this class matches the <tt class="docutils literal"><span class="pre">NEW</span></tt> statement.</li>
<li>This approach involves determining exactly which columns you really need,
and instantiating data-transfer object that containing a constructor with those arguments.</li>
</ul>
<p>If you want to select data-transfer objects you should create a class:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">class</span> <span class="nc">CustomerDTO</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$email</span><span class="p">,</span> <span class="nv">$city</span><span class="p">,</span> <span class="nv">$value</span> <span class="o">=</span> <span class="k">null</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// Bind values to the object properties.</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>And then use the <tt class="docutils literal"><span class="pre">NEW</span></tt> DQL keyword :</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT NEW CustomerDTO(c.name, e.email, a.city) FROM Customer c JOIN c.email e JOIN c.address a'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CustomerDTO</span>
</pre></div>
</div>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT NEW CustomerDTO(c.name, e.email, a.city, SUM(o.value)) FROM Customer c JOIN c.email e JOIN c.address a JOIN c.orders o GROUP BY c'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// array of CustomerDTO</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="using-index-by">
<h3>15.2.5. Using INDEX BY<a class="headerlink" href="#using-index-by" title="Permalink to this headline">¶</a></h3>
<p>The INDEX BY construct is nothing that directly translates into SQL
but that affects object and array hydration. After each FROM and
JOIN clause you specify by which field this class should be indexed
in the result. By default a result is incremented by numerical keys
starting with 0. However with INDEX BY you can specify any other
column to be the key of your result, it really only makes sense
with primary or unique fields though:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">SELECT</span> <span class="n">u</span><span class="p">.</span><span class="n">id</span><span class="p">,</span> <span class="n">u</span><span class="p">.</span><span class="n">status</span><span class="p">,</span> <span class="k">upper</span><span class="p">(</span><span class="n">u</span><span class="p">.</span><span class="n">name</span><span class="p">)</span> <span class="n">nameUpper</span> <span class="k">FROM</span> <span class="k">User</span> <span class="n">u</span> <span class="k">INDEX</span> <span class="k">BY</span> <span class="n">u</span><span class="p">.</span><span class="n">id</span>
<span class="k">JOIN</span> <span class="n">u</span><span class="p">.</span><span class="n">phonenumbers</span> <span class="n">p</span> <span class="k">INDEX</span> <span class="k">BY</span> <span class="n">p</span><span class="p">.</span><span class="n">phonenumber</span>
</pre></div>
</div>
<p>Returns an array of the following kind, indexed by both user-id
then phonenumber-id:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">array</span>
<span class="x"> 0 =></span>
<span class="x"> array</span>
<span class="x"> 1 =></span>
<span class="x"> object(stdClass)[299]</span>
<span class="x"> public '__CLASS__' => string 'Doctrine\Tests\Models\CMS\CmsUser' (length=33)</span>
<span class="x"> public 'id' => int 1</span>
<span class="x"> ..</span>
<span class="x"> 'nameUpper' => string 'ROMANB' (length=6)</span>
<span class="x"> 1 =></span>
<span class="x"> array</span>
<span class="x"> 2 =></span>
<span class="x"> object(stdClass)[298]</span>
<span class="x"> public '__CLASS__' => string 'Doctrine\Tests\Models\CMS\CmsUser' (length=33)</span>
<span class="x"> public 'id' => int 2</span>
<span class="x"> ...</span>
<span class="x"> 'nameUpper' => string 'JWAGE' (length=5)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="update-queries">
<h2>15.3. UPDATE queries<a class="headerlink" href="#update-queries" title="Permalink to this headline">¶</a></h2>
<p>DQL not only allows to select your Entities using field names, you
can also execute bulk updates on a set of entities using an
DQL-UPDATE query. The Syntax of an UPDATE query works as expected,
as the following example shows:</p>
<div class="highlight-sql"><div class="highlight"><pre>UPDATE MyProject\Model\User u SET u.password = 'new' WHERE u.id IN (1, 2, 3)
</pre></div>
</div>
<p>References to related entities are only possible in the WHERE
clause and using sub-selects.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">DQL UPDATE statements are ported directly into a
Database UPDATE statement and therefore bypass any locking scheme, events
and do not increment the version column. Entities that are already
loaded into the persistence context will <em>NOT</em> be synced with the
updated database state. It is recommended to call
<tt class="docutils literal"><span class="pre">EntityManager#clear()</span></tt> and retrieve new instances of any
affected entity.</p>
</div>
</div>
<div class="section" id="delete-queries">
<h2>15.4. DELETE queries<a class="headerlink" href="#delete-queries" title="Permalink to this headline">¶</a></h2>
<p>DELETE queries can also be specified using DQL and their syntax is
as simple as the UPDATE syntax:</p>
<div class="highlight-sql"><div class="highlight"><pre>DELETE MyProject\Model\User u WHERE u.id = 4
</pre></div>
</div>
<p>The same restrictions apply for the reference of related entities.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">DQL DELETE statements are ported directly into a
Database DELETE statement and therefore bypass any events and checks for the
version column if they are not explicitly added to the WHERE clause
of the query. Additionally Deletes of specifies entities are <em>NOT</em>
cascaded to related entities even if specified in the metadata.</p>
</div>
</div>
<div class="section" id="functions-operators-aggregates">
<h2>15.5. Functions, Operators, Aggregates<a class="headerlink" href="#functions-operators-aggregates" title="Permalink to this headline">¶</a></h2>
<div class="section" id="dql-functions">
<h3>15.5.1. DQL Functions<a class="headerlink" href="#dql-functions" title="Permalink to this headline">¶</a></h3>
<p>The following functions are supported in SELECT, WHERE and HAVING
clauses:</p>
<ul class="simple">
<li>IDENTITY(single_association_path_expression [, fieldMapping]) - Retrieve the foreign key column of association of the owning side</li>
<li>ABS(arithmetic_expression)</li>
<li>CONCAT(str1, str2)</li>
<li>CURRENT_DATE() - Return the current date</li>
<li>CURRENT_TIME() - Returns the current time</li>
<li>CURRENT_TIMESTAMP() - Returns a timestamp of the current date
and time.</li>
<li>LENGTH(str) - Returns the length of the given string</li>
<li>LOCATE(needle, haystack [, offset]) - Locate the first
occurrence of the substring in the string.</li>
<li>LOWER(str) - returns the string lowercased.</li>
<li>MOD(a, b) - Return a MOD b.</li>
<li>SIZE(collection) - Return the number of elements in the
specified collection</li>
<li>SQRT(q) - Return the square-root of q.</li>
<li>SUBSTRING(str, start [, length]) - Return substring of given
string.</li>
<li>TRIM([LEADING | TRAILING | BOTH] [‘trchar’ FROM] str) - Trim
the string by the given trim char, defaults to whitespaces.</li>
<li>UPPER(str) - Return the upper-case of the given string.</li>
<li>DATE_ADD(date, days, unit) - Add the number of days to a given date. (Supported units are DAY, MONTH)</li>
<li>DATE_SUB(date, days, unit) - Substract the number of days from a given date. (Supported units are DAY, MONTH)</li>
<li>DATE_DIFF(date1, date2) - Calculate the difference in days between date1-date2.</li>
</ul>
</div>
<div class="section" id="arithmetic-operators">
<h3>15.5.2. Arithmetic operators<a class="headerlink" href="#arithmetic-operators" title="Permalink to this headline">¶</a></h3>
<p>You can do math in DQL using numeric values, for example:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">SELECT</span> <span class="n">person</span><span class="p">.</span><span class="n">salary</span> <span class="o">*</span> <span class="mi">1</span><span class="p">.</span><span class="mi">5</span> <span class="k">FROM</span> <span class="n">CompanyPerson</span> <span class="n">person</span> <span class="k">WHERE</span> <span class="n">person</span><span class="p">.</span><span class="n">salary</span> <span class="o"><</span> <span class="mi">100000</span>
</pre></div>
</div>
</div>
<div class="section" id="aggregate-functions">
<h3>15.5.3. Aggregate Functions<a class="headerlink" href="#aggregate-functions" title="Permalink to this headline">¶</a></h3>
<p>The following aggregate functions are allowed in SELECT and GROUP
BY clauses: AVG, COUNT, MIN, MAX, SUM</p>
</div>
<div class="section" id="other-expressions">
<h3>15.5.4. Other Expressions<a class="headerlink" href="#other-expressions" title="Permalink to this headline">¶</a></h3>
<p>DQL offers a wide-range of additional expressions that are known
from SQL, here is a list of all the supported constructs:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">ALL/ANY/SOME</span></tt> - Used in a WHERE clause followed by a
sub-select this works like the equivalent constructs in SQL.</li>
<li><tt class="docutils literal"><span class="pre">BETWEEN</span> <span class="pre">a</span> <span class="pre">AND</span> <span class="pre">b</span></tt> and <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">BETWEEN</span> <span class="pre">a</span> <span class="pre">AND</span> <span class="pre">b</span></tt> can be used to
match ranges of arithmetic values.</li>
<li><tt class="docutils literal"><span class="pre">IN</span> <span class="pre">(x1,</span> <span class="pre">x2,</span> <span class="pre">...)</span></tt> and <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">IN</span> <span class="pre">(x1,</span> <span class="pre">x2,</span> <span class="pre">..)</span></tt> can be used to
match a set of given values.</li>
<li><tt class="docutils literal"><span class="pre">LIKE</span> <span class="pre">..</span></tt> and <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">LIKE</span> <span class="pre">..</span></tt> match parts of a string or text
using % as a wildcard.</li>
<li><tt class="docutils literal"><span class="pre">IS</span> <span class="pre">NULL</span></tt> and <tt class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span> <span class="pre">NULL</span></tt> to check for null values</li>
<li><tt class="docutils literal"><span class="pre">EXISTS</span></tt> and <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">EXISTS</span></tt> in combination with a sub-select</li>
</ul>
</div>
<div class="section" id="adding-your-own-functions-to-the-dql-language">
<h3>15.5.5. Adding your own functions to the DQL language<a class="headerlink" href="#adding-your-own-functions-to-the-dql-language" title="Permalink to this headline">¶</a></h3>
<p>By default DQL comes with functions that are part of a large basis
of underlying databases. However you will most likely choose a
database platform at the beginning of your project and most likely
never change it. For this cases you can easily extend the DQL
parser with own specialized platform functions.</p>
<p>You can register custom DQL functions in your ORM Configuration:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\ORM\Configuration</span><span class="p">();</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">addCustomStringFunction</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$class</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">addCustomNumericFunction</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$class</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">addCustomDatetimeFunction</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$class</span><span class="p">);</span>
<span class="nv">$em</span> <span class="o">=</span> <span class="nx">EntityManager</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nv">$dbParams</span><span class="p">,</span> <span class="nv">$config</span><span class="p">);</span>
</pre></div>
</div>
<p>The functions have to return either a string, numeric or datetime
value depending on the registered function type. As an example we
will add a MySQL specific FLOOR() functionality. All the given
classes have to implement the base class :</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">MyProject\Query\AST</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">\Doctrine\ORM\Query\AST\Functions\FunctionNode</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">\Doctrine\ORM\Query\Lexer</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">MysqlFloor</span> <span class="k">extends</span> <span class="nx">FunctionNode</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nv">$simpleArithmeticExpression</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getSql</span><span class="p">(</span><span class="nx">\Doctrine\ORM\Query\SqlWalker</span> <span class="nv">$sqlWalker</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="s1">'FLOOR('</span> <span class="o">.</span> <span class="nv">$sqlWalker</span><span class="o">-></span><span class="na">walkSimpleArithmeticExpression</span><span class="p">(</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">simpleArithmeticExpression</span>
<span class="p">)</span> <span class="o">.</span> <span class="s1">')'</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">parse</span><span class="p">(</span><span class="nx">\Doctrine\ORM\Query\Parser</span> <span class="nv">$parser</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$lexer</span> <span class="o">=</span> <span class="nv">$parser</span><span class="o">-></span><span class="na">getLexer</span><span class="p">();</span>
<span class="nv">$parser</span><span class="o">-></span><span class="na">match</span><span class="p">(</span><span class="nx">Lexer</span><span class="o">::</span><span class="na">T_IDENTIFIER</span><span class="p">);</span>
<span class="nv">$parser</span><span class="o">-></span><span class="na">match</span><span class="p">(</span><span class="nx">Lexer</span><span class="o">::</span><span class="na">T_OPEN_PARENTHESIS</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">simpleArithmeticExpression</span> <span class="o">=</span> <span class="nv">$parser</span><span class="o">-></span><span class="na">SimpleArithmeticExpression</span><span class="p">();</span>
<span class="nv">$parser</span><span class="o">-></span><span class="na">match</span><span class="p">(</span><span class="nx">Lexer</span><span class="o">::</span><span class="na">T_CLOSE_PARENTHESIS</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>We will register the function by calling and can then use it:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">getConfiguration</span><span class="p">();</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">registerNumericFunction</span><span class="p">(</span><span class="s1">'FLOOR'</span><span class="p">,</span> <span class="s1">'MyProject\Query\MysqlFloor'</span><span class="p">);</span>
<span class="nv">$dql</span> <span class="o">=</span> <span class="s2">"SELECT FLOOR(person.salary * 1.75) FROM CompanyPerson person"</span><span class="p">;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="querying-inherited-classes">
<h2>15.6. Querying Inherited Classes<a class="headerlink" href="#querying-inherited-classes" title="Permalink to this headline">¶</a></h2>
<p>This section demonstrates how you can query inherited classes and
what type of results to expect.</p>
<div class="section" id="single-table">
<h3>15.6.1. Single Table<a class="headerlink" href="#single-table" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="http://martinfowler.com/eaaCatalog/singleTableInheritance.html">Single Table Inheritance</a>
is an inheritance mapping strategy where all classes of a hierarchy
are mapped to a single database table. In order to distinguish
which row represents which type in the hierarchy a so-called
discriminator column is used.</p>
<p>First we need to setup an example set of entities to use. In this
scenario it is a generic Person and Employee example:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Entities</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @Entity</span>
<span class="sd"> * @InheritanceType("SINGLE_TABLE")</span>
<span class="sd"> * @DiscriminatorColumn(name="discr", type="string")</span>
<span class="sd"> * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">Person</span>
<span class="p">{</span>
<span class="sd">/**</span>
<span class="sd"> * @Id @Column(type="integer")</span>
<span class="sd"> * @GeneratedValue</span>
<span class="sd"> */</span>
<span class="k">protected</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @Column(type="string", length=50)</span>
<span class="sd"> */</span>
<span class="k">protected</span> <span class="nv">$name</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * @Entity</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">Employee</span> <span class="k">extends</span> <span class="nx">Person</span>
<span class="p">{</span>
<span class="sd">/**</span>
<span class="sd"> * @Column(type="string", length=50)</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$department</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>First notice that the generated SQL to create the tables for these
entities looks like the following:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">Person</span> <span class="p">(</span>
<span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="n">AUTOINCREMENT</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="n">discr</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="n">department</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Now when persist a new <tt class="docutils literal"><span class="pre">Employee</span></tt> instance it will set the
discriminator value for us automatically:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$employee</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Entities\Employee</span><span class="p">();</span>
<span class="nv">$employee</span><span class="o">-></span><span class="na">setName</span><span class="p">(</span><span class="s1">'test'</span><span class="p">);</span>
<span class="nv">$employee</span><span class="o">-></span><span class="na">setDepartment</span><span class="p">(</span><span class="s1">'testing'</span><span class="p">);</span>
<span class="nv">$em</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$employee</span><span class="p">);</span>
<span class="nv">$em</span><span class="o">-></span><span class="na">flush</span><span class="p">();</span>
</pre></div>
</div>
<p>Now lets run a simple query to retrieve the <tt class="docutils literal"><span class="pre">Employee</span></tt> we just
created:</p>
<div class="highlight-sql"><div class="highlight"><pre>SELECT e FROM Entities\Employee e WHERE e.name = 'test'
</pre></div>
</div>
<p>If we check the generated SQL you will notice it has some special
conditions added to ensure that we will only get back <tt class="docutils literal"><span class="pre">Employee</span></tt>
entities:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">SELECT</span> <span class="n">p0_</span><span class="p">.</span><span class="n">id</span> <span class="k">AS</span> <span class="n">id0</span><span class="p">,</span> <span class="n">p0_</span><span class="p">.</span><span class="n">name</span> <span class="k">AS</span> <span class="n">name1</span><span class="p">,</span> <span class="n">p0_</span><span class="p">.</span><span class="n">department</span> <span class="k">AS</span> <span class="n">department2</span><span class="p">,</span>
<span class="n">p0_</span><span class="p">.</span><span class="n">discr</span> <span class="k">AS</span> <span class="n">discr3</span> <span class="k">FROM</span> <span class="n">Person</span> <span class="n">p0_</span>
<span class="k">WHERE</span> <span class="p">(</span><span class="n">p0_</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="o">?</span><span class="p">)</span> <span class="k">AND</span> <span class="n">p0_</span><span class="p">.</span><span class="n">discr</span> <span class="k">IN</span> <span class="p">(</span><span class="s1">'employee'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="class-table-inheritance">
<h3>15.6.2. Class Table Inheritance<a class="headerlink" href="#class-table-inheritance" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="http://martinfowler.com/eaaCatalog/classTableInheritance.html">Class Table Inheritance</a>
is an inheritance mapping strategy where each class in a hierarchy
is mapped to several tables: its own table and the tables of all
parent classes. The table of a child class is linked to the table
of a parent class through a foreign key constraint. Doctrine 2
implements this strategy through the use of a discriminator column
in the topmost table of the hierarchy because this is the easiest
way to achieve polymorphic queries with Class Table Inheritance.</p>
<p>The example for class table inheritance is the same as single
table, you just need to change the inheritance type from
<tt class="docutils literal"><span class="pre">SINGLE_TABLE</span></tt> to <tt class="docutils literal"><span class="pre">JOINED</span></tt>:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="sd">/**</span>
<span class="sd"> * @Entity</span>
<span class="sd"> * @InheritanceType("JOINED")</span>
<span class="sd"> * @DiscriminatorColumn(name="discr", type="string")</span>
<span class="sd"> * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">Person</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Now take a look at the SQL which is generated to create the table,
you’ll notice some differences:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">Person</span> <span class="p">(</span>
<span class="n">id</span> <span class="nb">INT</span> <span class="n">AUTO_INCREMENT</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="n">discr</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
<span class="p">)</span> <span class="n">ENGINE</span> <span class="o">=</span> <span class="n">InnoDB</span><span class="p">;</span>
<span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">Employee</span> <span class="p">(</span>
<span class="n">id</span> <span class="nb">INT</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="n">department</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
<span class="p">)</span> <span class="n">ENGINE</span> <span class="o">=</span> <span class="n">InnoDB</span><span class="p">;</span>
<span class="k">ALTER</span> <span class="k">TABLE</span> <span class="n">Employee</span> <span class="k">ADD</span> <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="p">(</span><span class="n">id</span><span class="p">)</span> <span class="k">REFERENCES</span> <span class="n">Person</span><span class="p">(</span><span class="n">id</span><span class="p">)</span> <span class="k">ON</span> <span class="k">DELETE</span> <span class="k">CASCADE</span>
</pre></div>
</div>
<ul class="simple">
<li>The data is split between two tables</li>
<li>A foreign key exists between the two tables</li>
</ul>
<p>Now if were to insert the same <tt class="docutils literal"><span class="pre">Employee</span></tt> as we did in the
<tt class="docutils literal"><span class="pre">SINGLE_TABLE</span></tt> example and run the same example query it will
generate different SQL joining the <tt class="docutils literal"><span class="pre">Person</span></tt> information
automatically for you:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">SELECT</span> <span class="n">p0_</span><span class="p">.</span><span class="n">id</span> <span class="k">AS</span> <span class="n">id0</span><span class="p">,</span> <span class="n">p0_</span><span class="p">.</span><span class="n">name</span> <span class="k">AS</span> <span class="n">name1</span><span class="p">,</span> <span class="n">e1_</span><span class="p">.</span><span class="n">department</span> <span class="k">AS</span> <span class="n">department2</span><span class="p">,</span>
<span class="n">p0_</span><span class="p">.</span><span class="n">discr</span> <span class="k">AS</span> <span class="n">discr3</span>
<span class="k">FROM</span> <span class="n">Employee</span> <span class="n">e1_</span> <span class="k">INNER</span> <span class="k">JOIN</span> <span class="n">Person</span> <span class="n">p0_</span> <span class="k">ON</span> <span class="n">e1_</span><span class="p">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">p0_</span><span class="p">.</span><span class="n">id</span>
<span class="k">WHERE</span> <span class="n">p0_</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="o">?</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="the-query-class">
<h2>15.7. The Query class<a class="headerlink" href="#the-query-class" title="Permalink to this headline">¶</a></h2>
<p>An instance of the <tt class="docutils literal"><span class="pre">Doctrine\ORM\Query</span></tt> class represents a DQL
query. You create a Query instance be calling
<tt class="docutils literal"><span class="pre">EntityManager#createQuery($dql)</span></tt>, passing the DQL query string.
Alternatively you can create an empty <tt class="docutils literal"><span class="pre">Query</span></tt> instance and invoke
<tt class="docutils literal"><span class="pre">Query#setDql($dql)</span></tt> afterwards. Here are some examples:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">// $em instanceof EntityManager</span>
<span class="c1">// example1: passing a DQL string</span>
<span class="nv">$q</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'select u from MyProject\Model\User u'</span><span class="p">);</span>
<span class="c1">// example2: using setDql</span>
<span class="nv">$q</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">();</span>
<span class="nv">$q</span><span class="o">-></span><span class="na">setDql</span><span class="p">(</span><span class="s1">'select u from MyProject\Model\User u'</span><span class="p">);</span>
</pre></div>
</div>
<div class="section" id="query-result-formats">
<h3>15.7.1. Query Result Formats<a class="headerlink" href="#query-result-formats" title="Permalink to this headline">¶</a></h3>
<p>The format in which the result of a DQL SELECT query is returned
can be influenced by a so-called <tt class="docutils literal"><span class="pre">hydration</span> <span class="pre">mode</span></tt>. A hydration
mode specifies a particular way in which a SQL result set is
transformed. Each hydration mode has its own dedicated method on
the Query class. Here they are:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">Query#getResult()</span></tt>: Retrieves a collection of objects. The
result is either a plain collection of objects (pure) or an array
where the objects are nested in the result rows (mixed).</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">Query#getSingleResult()</span></tt>: Retrieves a single object. If the
result contains more than one or no object, an exception is thrown. The
pure/mixed distinction does not apply.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">Query#getOneOrNullResult()</span></tt>: Retrieve a single object. If no
object is found null will be returned.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">Query#getArrayResult()</span></tt>: Retrieves an array graph (a nested
array) that is largely interchangeable with the object graph
generated by <tt class="docutils literal"><span class="pre">Query#getResult()</span></tt> for read-only purposes.</p>
<blockquote>
<div><div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">An array graph can differ from the corresponding object
graph in certain scenarios due to the difference of the identity
semantics between arrays and objects.</p>
</div>
</div></blockquote>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">Query#getScalarResult()</span></tt>: Retrieves a flat/rectangular result
set of scalar values that can contain duplicate data. The
pure/mixed distinction does not apply.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">Query#getSingleScalarResult()</span></tt>: Retrieves a single scalar
value from the result returned by the dbms. If the result contains
more than a single scalar value, an exception is thrown. The
pure/mixed distinction does not apply.</p>
</li>
</ul>
<p>Instead of using these methods, you can alternatively use the
general-purpose method
<tt class="docutils literal"><span class="pre">Query#execute(array</span> <span class="pre">$params</span> <span class="pre">=</span> <span class="pre">array(),</span> <span class="pre">$hydrationMode</span> <span class="pre">=</span> <span class="pre">Query::HYDRATE_OBJECT)</span></tt>.
Using this method you can directly supply the hydration mode as the
second parameter via one of the Query constants. In fact, the
methods mentioned earlier are just convenient shortcuts for the
execute method. For example, the method <tt class="docutils literal"><span class="pre">Query#getResult()</span></tt>
internally invokes execute, passing in <tt class="docutils literal"><span class="pre">Query::HYDRATE_OBJECT</span></tt> as
the hydration mode.</p>
<p>The use of the methods mentioned earlier is generally preferred as
it leads to more concise code.</p>
</div>
<div class="section" id="pure-and-mixed-results">
<h3>15.7.2. Pure and Mixed Results<a class="headerlink" href="#pure-and-mixed-results" title="Permalink to this headline">¶</a></h3>
<p>The nature of a result returned by a DQL SELECT query retrieved
through <tt class="docutils literal"><span class="pre">Query#getResult()</span></tt> or <tt class="docutils literal"><span class="pre">Query#getArrayResult()</span></tt> can be
of 2 forms: <strong>pure</strong> and <strong>mixed</strong>. In the previous simple
examples, you already saw a “pure” query result, with only objects.
By default, the result type is <strong>pure</strong> but
<strong>as soon as scalar values, such as aggregate values or other scalar values that do not belong to an entity, appear in the SELECT part of the DQL query, the result becomes mixed</strong>.
A mixed result has a different structure than a pure result in
order to accommodate for the scalar values.</p>
<p>A pure result usually looks like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">$dql = "SELECT u FROM User u";</span>
<span class="x">array</span>
<span class="x"> [0] => Object</span>
<span class="x"> [1] => Object</span>
<span class="x"> [2] => Object</span>
<span class="x"> ...</span>
</pre></div>
</div>
<p>A mixed result on the other hand has the following general
structure:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">$dql = "SELECT u, 'some scalar string', count(u.groups) AS num FROM User u JOIN u.groups g GROUP BY u.id";</span>
<span class="x">array</span>
<span class="x"> [0]</span>
<span class="x"> [0] => Object</span>
<span class="x"> [1] => "some scalar string"</span>
<span class="x"> ['num'] => 42</span>
<span class="x"> // ... more scalar values, either indexed numerically or with a name</span>
<span class="x"> [1]</span>
<span class="x"> [0] => Object</span>
<span class="x"> [1] => "some scalar string"</span>
<span class="x"> ['num'] => 42</span>
<span class="x"> // ... more scalar values, either indexed numerically or with a name</span>
</pre></div>
</div>
<p>To better understand mixed results, consider the following DQL
query:</p>
<div class="highlight-sql"><div class="highlight"><pre>SELECT u, UPPER(u.name) nameUpper FROM MyProject\Model\User u
</pre></div>
</div>
<p>This query makes use of the <tt class="docutils literal"><span class="pre">UPPER</span></tt> DQL function that returns a
scalar value and because there is now a scalar value in the SELECT
clause, we get a mixed result.</p>
<p>Conventions for mixed results are as follows:</p>
<ul class="simple">
<li>The object fetched in the FROM clause is always positioned with the key ‘0’.</li>
<li>Every scalar without a name is numbered in the order given in the query, starting with 1.</li>
<li>Every aliased scalar is given with its alias-name as the key. The case of the name is kept.</li>
<li>If several objects are fetched from the FROM clause they alternate every row.</li>
</ul>
<p>Here is how the result could look like:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">array</span>
<span class="x"> array</span>
<span class="x"> [0] => User (Object)</span>
<span class="x"> ['nameUpper'] => "ROMAN"</span>
<span class="x"> array</span>
<span class="x"> [0] => User (Object)</span>
<span class="x"> ['nameUpper'] => "JONATHAN"</span>
<span class="x"> ...</span>
</pre></div>
</div>
<p>And here is how you would access it in PHP code:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$results</span> <span class="k">as</span> <span class="nv">$row</span><span class="p">)</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"Name: "</span> <span class="o">.</span> <span class="nv">$row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-></span><span class="na">getName</span><span class="p">();</span>
<span class="k">echo</span> <span class="s2">"Name UPPER: "</span> <span class="o">.</span> <span class="nv">$row</span><span class="p">[</span><span class="s1">'nameUpper'</span><span class="p">];</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="fetching-multiple-from-entities">
<h3>15.7.3. Fetching Multiple FROM Entities<a class="headerlink" href="#fetching-multiple-from-entities" title="Permalink to this headline">¶</a></h3>
<p>If you fetch multiple entities that are listed in the FROM clause then the hydration
will return the rows iterating the different top-level entities.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">$dql = "SELECT u, g FROM User u, Group g";</span>
<span class="x">array</span>
<span class="x"> [0] => Object (User)</span>
<span class="x"> [1] => Object (Group)</span>
<span class="x"> [2] => Object (User)</span>
<span class="x"> [3] => Object (Group)</span>
</pre></div>
</div>
</div>
<div class="section" id="hydration-modes">
<h3>15.7.4. Hydration Modes<a class="headerlink" href="#hydration-modes" title="Permalink to this headline">¶</a></h3>
<p>Each of the Hydration Modes makes assumptions about how the result
is returned to user land. You should know about all the details to
make best use of the different result formats:</p>
<p>The constants for the different hydration modes are:</p>
<ul class="simple">
<li>Query::HYDRATE_OBJECT</li>
<li>Query::HYDRATE_ARRAY</li>
<li>Query::HYDRATE_SCALAR</li>
<li>Query::HYDRATE_SINGLE_SCALAR</li>
</ul>
<div class="section" id="object-hydration">
<h4>15.7.4.1. Object Hydration<a class="headerlink" href="#object-hydration" title="Permalink to this headline">¶</a></h4>
<p>Object hydration hydrates the result set into the object graph:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">(</span><span class="nx">Query</span><span class="o">::</span><span class="na">HYDRATE_OBJECT</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="array-hydration">
<h4>15.7.4.2. Array Hydration<a class="headerlink" href="#array-hydration" title="Permalink to this headline">¶</a></h4>
<p>You can run the same query with array hydration and the result set
is hydrated into an array that represents the object graph:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">(</span><span class="nx">Query</span><span class="o">::</span><span class="na">HYDRATE_ARRAY</span><span class="p">);</span>
</pre></div>
</div>
<p>You can use the <tt class="docutils literal"><span class="pre">getArrayResult()</span></tt> shortcut as well:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getArrayResult</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="scalar-hydration">
<h4>15.7.4.3. Scalar Hydration<a class="headerlink" href="#scalar-hydration" title="Permalink to this headline">¶</a></h4>
<p>If you want to return a flat rectangular result set instead of an
object graph you can use scalar hydration:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">(</span><span class="nx">Query</span><span class="o">::</span><span class="na">HYDRATE_SCALAR</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$users</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">'u_id'</span><span class="p">];</span>
</pre></div>
</div>
<p>The following assumptions are made about selected fields using
Scalar Hydration:</p>
<ol class="arabic simple">
<li>Fields from classes are prefixed by the DQL alias in the result.
A query of the kind ‘SELECT u.name ..’ returns a key ‘u_name’ in
the result rows.</li>
</ol>
</div>
<div class="section" id="single-scalar-hydration">
<h4>15.7.4.4. Single Scalar Hydration<a class="headerlink" href="#single-scalar-hydration" title="Permalink to this headline">¶</a></h4>
<p>If you a query which returns just a single scalar value you can use
single scalar hydration:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT COUNT(a.id) FROM CmsUser u LEFT JOIN u.articles a WHERE u.username = ?1 GROUP BY u.id'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'jwage'</span><span class="p">);</span>
<span class="nv">$numArticles</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">(</span><span class="nx">Query</span><span class="o">::</span><span class="na">HYDRATE_SINGLE_SCALAR</span><span class="p">);</span>
</pre></div>
</div>
<p>You can use the <tt class="docutils literal"><span class="pre">getSingleScalarResult()</span></tt> shortcut as well:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$numArticles</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getSingleScalarResult</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-hydration-modes">
<h4>15.7.4.5. Custom Hydration Modes<a class="headerlink" href="#custom-hydration-modes" title="Permalink to this headline">¶</a></h4>
<p>You can easily add your own custom hydration modes by first
creating a class which extends <tt class="docutils literal"><span class="pre">AbstractHydrator</span></tt>:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">MyProject\Hydrators</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ORM\Internal\Hydration\AbstractHydrator</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">CustomHydrator</span> <span class="k">extends</span> <span class="nx">AbstractHydrator</span>
<span class="p">{</span>
<span class="k">protected</span> <span class="k">function</span> <span class="nf">_hydrateAll</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">_stmt</span><span class="o">-></span><span class="na">fetchAll</span><span class="p">(</span><span class="nx">PDO</span><span class="o">::</span><span class="na">FETCH_ASSOC</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Next you just need to add the class to the ORM configuration:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$em</span><span class="o">-></span><span class="na">getConfiguration</span><span class="p">()</span><span class="o">-></span><span class="na">addCustomHydrationMode</span><span class="p">(</span><span class="s1">'CustomHydrator'</span><span class="p">,</span> <span class="s1">'MyProject\Hydrators\CustomHydrator'</span><span class="p">);</span>
</pre></div>
</div>
<p>Now the hydrator is ready to be used in your queries:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM CmsUser u'</span><span class="p">);</span>
<span class="nv">$results</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">(</span><span class="s1">'CustomHydrator'</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="iterating-large-result-sets">
<h3>15.7.5. Iterating Large Result Sets<a class="headerlink" href="#iterating-large-result-sets" title="Permalink to this headline">¶</a></h3>
<p>There are situations when a query you want to execute returns a
very large result-set that needs to be processed. All the
previously described hydration modes completely load a result-set
into memory which might not be feasible with large result sets. See
the <a class="reference external" href="batch-processing">Batch Processing</a> section on details how
to iterate large result sets.</p>
</div>
<div class="section" id="functions">
<h3>15.7.6. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
<p>The following methods exist on the <tt class="docutils literal"><span class="pre">AbstractQuery</span></tt> which both
<tt class="docutils literal"><span class="pre">Query</span></tt> and <tt class="docutils literal"><span class="pre">NativeQuery</span></tt> extend from.</p>
<div class="section" id="parameters">
<h4>15.7.6.1. Parameters<a class="headerlink" href="#parameters" title="Permalink to this headline">¶</a></h4>
<p>Prepared Statements that use numerical or named wildcards require
additional parameters to be executable against the database. To
pass parameters to the query the following methods can be used:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">AbstractQuery::setParameter($param,</span> <span class="pre">$value)</span></tt> - Set the
numerical or named wildcard to the given value.</li>
<li><tt class="docutils literal"><span class="pre">AbstractQuery::setParameters(array</span> <span class="pre">$params)</span></tt> - Set an array
of parameter key-value pairs.</li>
<li><tt class="docutils literal"><span class="pre">AbstractQuery::getParameter($param)</span></tt></li>
<li><tt class="docutils literal"><span class="pre">AbstractQuery::getParameters()</span></tt></li>
</ul>
<p>Both named and positional parameters are passed to these methods without their ? or : prefix.</p>
</div>
<div class="section" id="cache-related-api">
<h4>15.7.6.2. Cache related API<a class="headerlink" href="#cache-related-api" title="Permalink to this headline">¶</a></h4>
<p>You can cache query results based either on all variables that
define the result (SQL, Hydration Mode, Parameters and Hints) or on
user-defined cache keys. However by default query results are not
cached at all. You have to enable the result cache on a per query
basis. The following example shows a complete workflow using the
Result Cache API:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s1">'SELECT u FROM MyProject\Model\User u WHERE u.id = ?1'</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setResultCacheDriver</span><span class="p">(</span><span class="k">new</span> <span class="nx">ApcCache</span><span class="p">());</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">useResultCache</span><span class="p">(</span><span class="k">true</span><span class="p">)</span>
<span class="o">-></span><span class="na">setResultCacheLifeTime</span><span class="p">(</span><span class="nv">$seconds</span> <span class="o">=</span> <span class="mi">3600</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// cache miss</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">expireResultCache</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// forced expire, cache miss</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setResultCacheId</span><span class="p">(</span><span class="s1">'my_query_result'</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// saved in given result cache id.</span>
<span class="c1">// or call useResultCache() with all parameters:</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">useResultCache</span><span class="p">(</span><span class="k">true</span><span class="p">,</span> <span class="nv">$seconds</span> <span class="o">=</span> <span class="mi">3600</span><span class="p">,</span> <span class="s1">'my_query_result'</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResult</span><span class="p">();</span> <span class="c1">// cache hit!</span>
<span class="c1">// Introspection</span>
<span class="nv">$queryCacheProfile</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getQueryCacheProfile</span><span class="p">();</span>
<span class="nv">$cacheDriver</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getResultCacheDriver</span><span class="p">();</span>
<span class="nv">$lifetime</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getLifetime</span><span class="p">();</span>
<span class="nv">$key</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">getCacheKey</span><span class="p">();</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You can set the Result Cache Driver globally on the
<tt class="docutils literal"><span class="pre">Doctrine\ORM\Configuration</span></tt> instance so that it is passed to
every <tt class="docutils literal"><span class="pre">Query</span></tt> and <tt class="docutils literal"><span class="pre">NativeQuery</span></tt> instance.</p>
</div>
</div>
<div class="section" id="query-hints">
<h4>15.7.6.3. Query Hints<a class="headerlink" href="#query-hints" title="Permalink to this headline">¶</a></h4>
<p>You can pass hints to the query parser and hydrators by using the
<tt class="docutils literal"><span class="pre">AbstractQuery::setHint($name,</span> <span class="pre">$value)</span></tt> method. Currently there
exist mostly internal query hints that are not be consumed in
userland. However the following few hints are to be used in
userland:</p>
<ul class="simple">
<li>Query::HINT_FORCE_PARTIAL_LOAD - Allows to hydrate objects
although not all their columns are fetched. This query hint can be
used to handle memory consumption problems with large result-sets
that contain char or binary data. Doctrine has no way of implicitly
reloading this data. Partially loaded objects have to be passed to
<tt class="docutils literal"><span class="pre">EntityManager::refresh()</span></tt> if they are to be reloaded fully from
the database.</li>
<li>Query::HINT_REFRESH - This query is used internally by
<tt class="docutils literal"><span class="pre">EntityManager::refresh()</span></tt> and can be used in userland as well.
If you specify this hint and a query returns the data for an entity
that is already managed by the UnitOfWork, the fields of the
existing entity will be refreshed. In normal operation a result-set
that loads data of an already existing entity is discarded in favor
of the already existing entity.</li>
<li>Query::HINT_CUSTOM_TREE_WALKERS - An array of additional
<tt class="docutils literal"><span class="pre">Doctrine\ORM\Query\TreeWalker</span></tt> instances that are attached to
the DQL query parsing process.</li>
</ul>
</div>
<div class="section" id="query-cache-dql-query-only">
<h4>15.7.6.4. Query Cache (DQL Query Only)<a class="headerlink" href="#query-cache-dql-query-only" title="Permalink to this headline">¶</a></h4>
<p>Parsing a DQL query and converting it into a SQL query against the
underlying database platform obviously has some overhead in
contrast to directly executing Native SQL queries. That is why
there is a dedicated Query Cache for caching the DQL parser
results. In combination with the use of wildcards you can reduce
the number of parsed queries in production to zero.</p>
<p>The Query Cache Driver is passed from the
<tt class="docutils literal"><span class="pre">Doctrine\ORM\Configuration</span></tt> instance to each
<tt class="docutils literal"><span class="pre">Doctrine\ORM\Query</span></tt> instance by default and is also enabled by
default. This also means you don’t regularly need to fiddle with
the parameters of the Query Cache, however if you do there are
several methods to interact with it:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Query::setQueryCacheDriver($driver)</span></tt> - Allows to set a Cache
instance</li>
<li><tt class="docutils literal"><span class="pre">Query::setQueryCacheLifeTime($seconds</span> <span class="pre">=</span> <span class="pre">3600)</span></tt> - Set lifetime
of the query caching.</li>
<li><tt class="docutils literal"><span class="pre">Query::expireQueryCache($bool)</span></tt> - Enforce the expiring of the
query cache if set to true.</li>
<li><tt class="docutils literal"><span class="pre">Query::getExpireQueryCache()</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Query::getQueryCacheDriver()</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Query::getQueryCacheLifeTime()</span></tt></li>
</ul>
</div>
<div class="section" id="first-and-max-result-items-dql-query-only">
<h4>15.7.6.5. First and Max Result Items (DQL Query Only)<a class="headerlink" href="#first-and-max-result-items-dql-query-only" title="Permalink to this headline">¶</a></h4>
<p>You can limit the number of results returned from a DQL query as
well as specify the starting offset, Doctrine then uses a strategy
of manipulating the select query to return only the requested
number of results:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Query::setMaxResults($maxResults)</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Query::setFirstResult($offset)</span></tt></li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If your query contains a fetch-joined collection
specifying the result limit methods are not working as you would
expect. Set Max Results restricts the number of database result
rows, however in the case of fetch-joined collections one root
entity might appear in many rows, effectively hydrating less than
the specified number of results.</p>
</div>
</div>
<div class="section" id="temporarily-change-fetch-mode-in-dql">
<span id="dql-temporarily-change-fetch-mode"></span><h4>15.7.6.6. Temporarily change fetch mode in DQL<a class="headerlink" href="#temporarily-change-fetch-mode-in-dql" title="Permalink to this headline">¶</a></h4>
<p>While normally all your associations are marked as lazy or extra lazy you will have cases where you are using DQL and don’t want to
fetch join a second, third or fourth level of entities into your result, because of the increased cost of the SQL JOIN. You
can mark a many-to-one or one-to-one association as fetched temporarily to batch fetch these entities using a WHERE .. IN query.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">createQuery</span><span class="p">(</span><span class="s2">"SELECT u FROM MyProject\User u"</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">setFetchMode</span><span class="p">(</span><span class="s2">"MyProject\User"</span><span class="p">,</span> <span class="s2">"address"</span><span class="p">,</span> <span class="s2">"EAGER"</span><span class="p">);</span>
<span class="nv">$query</span><span class="o">-></span><span class="na">execute</span><span class="p">();</span>
</pre></div>
</div>
<p>Given that there are 10 users and corresponding addresses in the database the executed queries will look something like:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">SELECT</span> <span class="o">*</span> <span class="k">FROM</span> <span class="n">users</span><span class="p">;</span>
<span class="k">SELECT</span> <span class="o">*</span> <span class="k">FROM</span> <span class="n">address</span> <span class="k">WHERE</span> <span class="n">id</span> <span class="k">IN</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="ebnf">
<h2>15.8. EBNF<a class="headerlink" href="#ebnf" title="Permalink to this headline">¶</a></h2>
<p>The following context-free grammar, written in an EBNF variant,
describes the Doctrine Query Language. You can consult this grammar
whenever you are unsure about what is possible with DQL or what the
correct syntax for a particular query should be.</p>
<div class="section" id="document-syntax">
<h3>15.8.1. Document syntax:<a class="headerlink" href="#document-syntax" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>non-terminals begin with an upper case character</li>
<li>terminals begin with a lower case character</li>
<li>parentheses (...) are used for grouping</li>
<li>square brackets [...] are used for defining an optional part,
e.g. zero or one time</li>
<li>curly brackets {...} are used for repetition, e.g. zero or more
times</li>
<li>double quotation marks ”...” define a terminal string a vertical
bar | represents an alternative</li>
</ul>
</div>
<div class="section" id="terminals">
<h3>15.8.2. Terminals<a class="headerlink" href="#terminals" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>identifier (name, email, ...)</li>
<li>string (‘foo’, ‘bar’’s house’, ‘%ninja%’, ...)</li>
<li>char (‘/’, ‘\’, ‘ ‘, ...)</li>
<li>integer (-1, 0, 1, 34, ...)</li>
<li>float (-0.23, 0.007, 1.245342E+8, ...)</li>
<li>boolean (false, true)</li>
</ul>
</div>
<div class="section" id="query-language">
<h3>15.8.3. Query Language<a class="headerlink" href="#query-language" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">QueryLanguage ::= SelectStatement | UpdateStatement | DeleteStatement</span>
</pre></div>
</div>
</div>
<div class="section" id="statements">
<h3>15.8.4. Statements<a class="headerlink" href="#statements" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">SelectStatement ::= SelectClause FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]</span>
<span class="x">UpdateStatement ::= UpdateClause [WhereClause]</span>
<span class="x">DeleteStatement ::= DeleteClause [WhereClause]</span>
</pre></div>
</div>
</div>
<div class="section" id="identifiers">
<h3>15.8.5. Identifiers<a class="headerlink" href="#identifiers" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">/* Alias Identification usage (the "u" of "u.name") */</span>
<span class="x">IdentificationVariable ::= identifier</span>
<span class="x">/* Alias Identification declaration (the "u" of "FROM User u") */</span>
<span class="x">AliasIdentificationVariable :: = identifier</span>
<span class="x">/* identifier that must be a class name (the "User" of "FROM User u") */</span>
<span class="x">AbstractSchemaName ::= identifier</span>
<span class="x">/* identifier that must be a field (the "name" of "u.name") */</span>
<span class="x">/* This is responsible to know if the field exists in Object, no matter if it's a relation or a simple field */</span>
<span class="x">FieldIdentificationVariable ::= identifier</span>
<span class="x">/* identifier that must be a collection-valued association field (to-many) (the "Phonenumbers" of "u.Phonenumbers") */</span>
<span class="x">CollectionValuedAssociationField ::= FieldIdentificationVariable</span>
<span class="x">/* identifier that must be a single-valued association field (to-one) (the "Group" of "u.Group") */</span>
<span class="x">SingleValuedAssociationField ::= FieldIdentificationVariable</span>
<span class="x">/* identifier that must be an embedded class state field (for the future) */</span>
<span class="x">EmbeddedClassStateField ::= FieldIdentificationVariable</span>
<span class="x">/* identifier that must be a simple state field (name, email, ...) (the "name" of "u.name") */</span>
<span class="x">/* The difference between this and FieldIdentificationVariable is only semantical, because it points to a single field (not mapping to a relation) */</span>
<span class="x">SimpleStateField ::= FieldIdentificationVariable</span>
<span class="x">/* Alias ResultVariable declaration (the "total" of "COUNT(*) AS total") */</span>
<span class="x">AliasResultVariable = identifier</span>
<span class="x">/* ResultVariable identifier usage of mapped field aliases (the "total" of "COUNT(*) AS total") */</span>
<span class="x">ResultVariable = identifier</span>
</pre></div>
</div>
</div>
<div class="section" id="path-expressions">
<h3>15.8.6. Path Expressions<a class="headerlink" href="#path-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">/* "u.Group" or "u.Phonenumbers" declarations */</span>
<span class="x">JoinAssociationPathExpression ::= IdentificationVariable "." (CollectionValuedAssociationField | SingleValuedAssociationField)</span>
<span class="x">/* "u.Group" or "u.Phonenumbers" usages */</span>
<span class="x">AssociationPathExpression ::= CollectionValuedPathExpression | SingleValuedAssociationPathExpression</span>
<span class="x">/* "u.name" or "u.Group" */</span>
<span class="x">SingleValuedPathExpression ::= StateFieldPathExpression | SingleValuedAssociationPathExpression</span>
<span class="x">/* "u.name" or "u.Group.name" */</span>
<span class="x">StateFieldPathExpression ::= IdentificationVariable "." StateField</span>
<span class="x">/* "u.Group" */</span>
<span class="x">SingleValuedAssociationPathExpression ::= IdentificationVariable "." SingleValuedAssociationField</span>
<span class="x">/* "u.Group.Permissions" */</span>
<span class="x">CollectionValuedPathExpression ::= IdentificationVariable "." CollectionValuedAssociationField</span>
<span class="x">/* "name" */</span>
<span class="x">StateField ::= {EmbeddedClassStateField "."}* SimpleStateField</span>
</pre></div>
</div>
</div>
<div class="section" id="clauses">
<h3>15.8.7. Clauses<a class="headerlink" href="#clauses" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}*</span>
<span class="x">SimpleSelectClause ::= "SELECT" ["DISTINCT"] SimpleSelectExpression</span>
<span class="x">UpdateClause ::= "UPDATE" AbstractSchemaName ["AS"] AliasIdentificationVariable "SET" UpdateItem {"," UpdateItem}*</span>
<span class="x">DeleteClause ::= "DELETE" ["FROM"] AbstractSchemaName ["AS"] AliasIdentificationVariable</span>
<span class="x">FromClause ::= "FROM" IdentificationVariableDeclaration {"," IdentificationVariableDeclaration}*</span>
<span class="x">SubselectFromClause ::= "FROM" SubselectIdentificationVariableDeclaration {"," SubselectIdentificationVariableDeclaration}*</span>
<span class="x">WhereClause ::= "WHERE" ConditionalExpression</span>
<span class="x">HavingClause ::= "HAVING" ConditionalExpression</span>
<span class="x">GroupByClause ::= "GROUP" "BY" GroupByItem {"," GroupByItem}*</span>
<span class="x">OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*</span>
<span class="x">Subselect ::= SimpleSelectClause SubselectFromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]</span>
</pre></div>
</div>
</div>
<div class="section" id="items">
<h3>15.8.8. Items<a class="headerlink" href="#items" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">UpdateItem ::= SingleValuedPathExpression "=" NewValue</span>
<span class="x">OrderByItem ::= (SimpleArithmeticExpression | SingleValuedPathExpression | ScalarExpression | ResultVariable) ["ASC" | "DESC"]</span>
<span class="x">GroupByItem ::= IdentificationVariable | ResultVariable | SingleValuedPathExpression</span>
<span class="x">NewValue ::= SimpleArithmeticExpression | "NULL"</span>
</pre></div>
</div>
</div>
<div class="section" id="from-join-and-index-by">
<h3>15.8.9. From, Join and Index by<a class="headerlink" href="#from-join-and-index-by" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">IdentificationVariableDeclaration ::= RangeVariableDeclaration [IndexBy] {JoinVariableDeclaration}*</span>
<span class="x">SubselectIdentificationVariableDeclaration ::= IdentificationVariableDeclaration | (AssociationPathExpression ["AS"] AliasIdentificationVariable)</span>
<span class="x">JoinVariableDeclaration ::= Join [IndexBy]</span>
<span class="x">RangeVariableDeclaration ::= AbstractSchemaName ["AS"] AliasIdentificationVariable</span>
<span class="x">Join ::= ["LEFT" ["OUTER"] | "INNER"] "JOIN" JoinAssociationPathExpression ["AS"] AliasIdentificationVariable ["WITH" ConditionalExpression]</span>
<span class="x">IndexBy ::= "INDEX" "BY" StateFieldPathExpression</span>
</pre></div>
</div>
</div>
<div class="section" id="select-expressions">
<h3>15.8.10. Select Expressions<a class="headerlink" href="#select-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">SelectExpression ::= (IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression) [["AS"] ["HIDDEN"] AliasResultVariable]</span>
<span class="x">SimpleSelectExpression ::= (StateFieldPathExpression | IdentificationVariable | FunctionDeclaration | AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable]</span>
<span class="x">PartialObjectExpression ::= "PARTIAL" IdentificationVariable "." PartialFieldSet</span>
<span class="x">PartialFieldSet ::= "{" SimpleStateField {"," SimpleStateField}* "}"</span>
</pre></div>
</div>
</div>
<div class="section" id="conditional-expressions">
<h3>15.8.11. Conditional Expressions<a class="headerlink" href="#conditional-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">ConditionalExpression ::= ConditionalTerm {"OR" ConditionalTerm}*</span>
<span class="x">ConditionalTerm ::= ConditionalFactor {"AND" ConditionalFactor}*</span>
<span class="x">ConditionalFactor ::= ["NOT"] ConditionalPrimary</span>
<span class="x">ConditionalPrimary ::= SimpleConditionalExpression | "(" ConditionalExpression ")"</span>
<span class="x">SimpleConditionalExpression ::= ComparisonExpression | BetweenExpression | LikeExpression |</span>
<span class="x"> InExpression | NullComparisonExpression | ExistsExpression |</span>
<span class="x"> EmptyCollectionComparisonExpression | CollectionMemberExpression |</span>
<span class="x"> InstanceOfExpression</span>
</pre></div>
</div>
</div>
<div class="section" id="collection-expressions">
<h3>15.8.12. Collection Expressions<a class="headerlink" href="#collection-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">EmptyCollectionComparisonExpression ::= CollectionValuedPathExpression "IS" ["NOT"] "EMPTY"</span>
<span class="x">CollectionMemberExpression ::= EntityExpression ["NOT"] "MEMBER" ["OF"] CollectionValuedPathExpression</span>
</pre></div>
</div>
</div>
<div class="section" id="literal-values">
<h3>15.8.13. Literal Values<a class="headerlink" href="#literal-values" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">Literal ::= string | char | integer | float | boolean</span>
<span class="x">InParameter ::= Literal | InputParameter</span>
</pre></div>
</div>
</div>
<div class="section" id="input-parameter">
<h3>15.8.14. Input Parameter<a class="headerlink" href="#input-parameter" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">InputParameter ::= PositionalParameter | NamedParameter</span>
<span class="x">PositionalParameter ::= "?" integer</span>
<span class="x">NamedParameter ::= ":" string</span>
</pre></div>
</div>
</div>
<div class="section" id="arithmetic-expressions">
<h3>15.8.15. Arithmetic Expressions<a class="headerlink" href="#arithmetic-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">ArithmeticExpression ::= SimpleArithmeticExpression | "(" Subselect ")"</span>
<span class="x">SimpleArithmeticExpression ::= ArithmeticTerm {("+" | "-") ArithmeticTerm}*</span>
<span class="x">ArithmeticTerm ::= ArithmeticFactor {("*" | "/") ArithmeticFactor}*</span>
<span class="x">ArithmeticFactor ::= [("+" | "-")] ArithmeticPrimary</span>
<span class="x">ArithmeticPrimary ::= SingleValuedPathExpression | Literal | "(" SimpleArithmeticExpression ")"</span>
<span class="x"> | FunctionsReturningNumerics | AggregateExpression | FunctionsReturningStrings</span>
<span class="x"> | FunctionsReturningDatetime | IdentificationVariable | ResultVariable</span>
<span class="x"> | InputParameter | CaseExpression</span>
</pre></div>
</div>
</div>
<div class="section" id="scalar-and-type-expressions">
<h3>15.8.16. Scalar and Type Expressions<a class="headerlink" href="#scalar-and-type-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">ScalarExpression ::= SimpleArithmeticExpression | StringPrimary | DateTimePrimary | StateFieldPathExpression | BooleanPrimary | CaseExpression | InstanceOfExpression</span>
<span class="x">StringExpression ::= StringPrimary | "(" Subselect ")"</span>
<span class="x">StringPrimary ::= StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression | CaseExpression</span>
<span class="x">BooleanExpression ::= BooleanPrimary | "(" Subselect ")"</span>
<span class="x">BooleanPrimary ::= StateFieldPathExpression | boolean | InputParameter</span>
<span class="x">EntityExpression ::= SingleValuedAssociationPathExpression | SimpleEntityExpression</span>
<span class="x">SimpleEntityExpression ::= IdentificationVariable | InputParameter</span>
<span class="x">DatetimeExpression ::= DatetimePrimary | "(" Subselect ")"</span>
<span class="x">DatetimePrimary ::= StateFieldPathExpression | InputParameter | FunctionsReturningDatetime | AggregateExpression</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Parts of CASE expressions are not yet implemented.</p>
</div>
</div>
<div class="section" id="aggregate-expressions">
<h3>15.8.17. Aggregate Expressions<a class="headerlink" href="#aggregate-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">AggregateExpression ::= ("AVG" | "MAX" | "MIN" | "SUM") "(" ["DISTINCT"] StateFieldPathExpression ")" |</span>
<span class="x"> "COUNT" "(" ["DISTINCT"] (IdentificationVariable | SingleValuedPathExpression) ")"</span>
</pre></div>
</div>
</div>
<div class="section" id="case-expressions">
<h3>15.8.18. Case Expressions<a class="headerlink" href="#case-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">CaseExpression ::= GeneralCaseExpression | SimpleCaseExpression | CoalesceExpression | NullifExpression</span>
<span class="x">GeneralCaseExpression ::= "CASE" WhenClause {WhenClause}* "ELSE" ScalarExpression "END"</span>
<span class="x">WhenClause ::= "WHEN" ConditionalExpression "THEN" ScalarExpression</span>
<span class="x">SimpleCaseExpression ::= "CASE" CaseOperand SimpleWhenClause {SimpleWhenClause}* "ELSE" ScalarExpression "END"</span>
<span class="x">CaseOperand ::= StateFieldPathExpression | TypeDiscriminator</span>
<span class="x">SimpleWhenClause ::= "WHEN" ScalarExpression "THEN" ScalarExpression</span>
<span class="x">CoalesceExpression ::= "COALESCE" "(" ScalarExpression {"," ScalarExpression}* ")"</span>
<span class="x">NullifExpression ::= "NULLIF" "(" ScalarExpression "," ScalarExpression ")"</span>
</pre></div>
</div>
</div>
<div class="section" id="id2">
<h3>15.8.19. Other Expressions<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<p>QUANTIFIED/BETWEEN/COMPARISON/LIKE/NULL/EXISTS</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">QuantifiedExpression ::= ("ALL" | "ANY" | "SOME") "(" Subselect ")"</span>
<span class="x">BetweenExpression ::= ArithmeticExpression ["NOT"] "BETWEEN" ArithmeticExpression "AND" ArithmeticExpression</span>
<span class="x">ComparisonExpression ::= ArithmeticExpression ComparisonOperator ( QuantifiedExpression | ArithmeticExpression )</span>
<span class="x">InExpression ::= SingleValuedPathExpression ["NOT"] "IN" "(" (InParameter {"," InParameter}* | Subselect) ")"</span>
<span class="x">InstanceOfExpression ::= IdentificationVariable ["NOT"] "INSTANCE" ["OF"] (InstanceOfParameter | "(" InstanceOfParameter {"," InstanceOfParameter}* ")")</span>
<span class="x">InstanceOfParameter ::= AbstractSchemaName | InputParameter</span>
<span class="x">LikeExpression ::= StringExpression ["NOT"] "LIKE" StringPrimary ["ESCAPE" char]</span>
<span class="x">NullComparisonExpression ::= (SingleValuedPathExpression | InputParameter) "IS" ["NOT"] "NULL"</span>
<span class="x">ExistsExpression ::= ["NOT"] "EXISTS" "(" Subselect ")"</span>
<span class="x">ComparisonOperator ::= "=" | "<" | "<=" | "<>" | ">" | ">=" | "!="</span>
</pre></div>
</div>
</div>
<div class="section" id="id3">
<h3>15.8.20. Functions<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="x">FunctionDeclaration ::= FunctionsReturningStrings | FunctionsReturningNumerics | FunctionsReturningDateTime</span>
<span class="x">FunctionsReturningNumerics ::=</span>
<span class="x"> "LENGTH" "(" StringPrimary ")" |</span>
<span class="x"> "LOCATE" "(" StringPrimary "," StringPrimary ["," SimpleArithmeticExpression]")" |</span>
<span class="x"> "ABS" "(" SimpleArithmeticExpression ")" |</span>
<span class="x"> "SQRT" "(" SimpleArithmeticExpression ")" |</span>
<span class="x"> "MOD" "(" SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |</span>
<span class="x"> "SIZE" "(" CollectionValuedPathExpression ")" |</span>
<span class="x"> "DATE_DIFF" "(" ArithmeticPrimary "," ArithmeticPrimary ")" |</span>
<span class="x"> "BIT_AND" "(" ArithmeticPrimary "," ArithmeticPrimary ")" |</span>
<span class="x"> "BIT_OR" "(" ArithmeticPrimary "," ArithmeticPrimary ")"</span>
<span class="x">FunctionsReturningDateTime ::=</span>
<span class="x"> "CURRENT_DATE" |</span>
<span class="x"> "CURRENT_TIME" |</span>
<span class="x"> "CURRENT_TIMESTAMP" |</span>
<span class="x"> "DATE_ADD" "(" ArithmeticPrimary "," ArithmeticPrimary "," StringPrimary ")" |</span>
<span class="x"> "DATE_SUB" "(" ArithmeticPrimary "," ArithmeticPrimary "," StringPrimary ")"</span>
<span class="x">FunctionsReturningStrings ::=</span>
<span class="x"> "CONCAT" "(" StringPrimary "," StringPrimary ")" |</span>
<span class="x"> "SUBSTRING" "(" StringPrimary "," SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |</span>
<span class="x"> "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" |</span>
<span class="x"> "LOWER" "(" StringPrimary ")" |</span>
<span class="x"> "UPPER" "(" StringPrimary ")" |</span>
<span class="x"> "IDENTITY" "(" SingleValuedAssociationPathExpression ")"</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="">
<h3>Search</h3>
<form class="search" action="http://readthedocs.org/search/project/" method="get">
<input type="text" name="q" size="18">
<input type="submit" value="Go">
<input type="hidden" name="selected_facets" value="project:">
</form>
</div>
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">15. Doctrine Query Language</a><ul>
<li><a class="reference internal" href="#types-of-dql-queries">15.1. Types of DQL queries</a></li>
<li><a class="reference internal" href="#select-queries">15.2. SELECT queries</a><ul>
<li><a class="reference internal" href="#dql-select-clause">15.2.1. DQL SELECT clause</a></li>
<li><a class="reference internal" href="#joins">15.2.2. Joins</a></li>
<li><a class="reference internal" href="#named-and-positional-parameters">15.2.3. Named and Positional Parameters</a></li>
<li><a class="reference internal" href="#dql-select-examples">15.2.4. DQL SELECT Examples</a><ul>
<li><a class="reference internal" href="#partial-object-syntax">15.2.4.1. Partial Object Syntax</a></li>
<li><a class="reference internal" href="#new-operator-syntax">15.2.4.2. “NEW” Operator Syntax</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-index-by">15.2.5. Using INDEX BY</a></li>
</ul>
</li>
<li><a class="reference internal" href="#update-queries">15.3. UPDATE queries</a></li>
<li><a class="reference internal" href="#delete-queries">15.4. DELETE queries</a></li>
<li><a class="reference internal" href="#functions-operators-aggregates">15.5. Functions, Operators, Aggregates</a><ul>
<li><a class="reference internal" href="#dql-functions">15.5.1. DQL Functions</a></li>
<li><a class="reference internal" href="#arithmetic-operators">15.5.2. Arithmetic operators</a></li>
<li><a class="reference internal" href="#aggregate-functions">15.5.3. Aggregate Functions</a></li>
<li><a class="reference internal" href="#other-expressions">15.5.4. Other Expressions</a></li>
<li><a class="reference internal" href="#adding-your-own-functions-to-the-dql-language">15.5.5. Adding your own functions to the DQL language</a></li>
</ul>
</li>
<li><a class="reference internal" href="#querying-inherited-classes">15.6. Querying Inherited Classes</a><ul>
<li><a class="reference internal" href="#single-table">15.6.1. Single Table</a></li>
<li><a class="reference internal" href="#class-table-inheritance">15.6.2. Class Table Inheritance</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-query-class">15.7. The Query class</a><ul>
<li><a class="reference internal" href="#query-result-formats">15.7.1. Query Result Formats</a></li>
<li><a class="reference internal" href="#pure-and-mixed-results">15.7.2. Pure and Mixed Results</a></li>
<li><a class="reference internal" href="#fetching-multiple-from-entities">15.7.3. Fetching Multiple FROM Entities</a></li>
<li><a class="reference internal" href="#hydration-modes">15.7.4. Hydration Modes</a><ul>
<li><a class="reference internal" href="#object-hydration">15.7.4.1. Object Hydration</a></li>
<li><a class="reference internal" href="#array-hydration">15.7.4.2. Array Hydration</a></li>
<li><a class="reference internal" href="#scalar-hydration">15.7.4.3. Scalar Hydration</a></li>
<li><a class="reference internal" href="#single-scalar-hydration">15.7.4.4. Single Scalar Hydration</a></li>
<li><a class="reference internal" href="#custom-hydration-modes">15.7.4.5. Custom Hydration Modes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#iterating-large-result-sets">15.7.5. Iterating Large Result Sets</a></li>
<li><a class="reference internal" href="#functions">15.7.6. Functions</a><ul>
<li><a class="reference internal" href="#parameters">15.7.6.1. Parameters</a></li>
<li><a class="reference internal" href="#cache-related-api">15.7.6.2. Cache related API</a></li>
<li><a class="reference internal" href="#query-hints">15.7.6.3. Query Hints</a></li>
<li><a class="reference internal" href="#query-cache-dql-query-only">15.7.6.4. Query Cache (DQL Query Only)</a></li>
<li><a class="reference internal" href="#first-and-max-result-items-dql-query-only">15.7.6.5. First and Max Result Items (DQL Query Only)</a></li>
<li><a class="reference internal" href="#temporarily-change-fetch-mode-in-dql">15.7.6.6. Temporarily change fetch mode in DQL</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#ebnf">15.8. EBNF</a><ul>
<li><a class="reference internal" href="#document-syntax">15.8.1. Document syntax:</a></li>
<li><a class="reference internal" href="#terminals">15.8.2. Terminals</a></li>
<li><a class="reference internal" href="#query-language">15.8.3. Query Language</a></li>
<li><a class="reference internal" href="#statements">15.8.4. Statements</a></li>
<li><a class="reference internal" href="#identifiers">15.8.5. Identifiers</a></li>
<li><a class="reference internal" href="#path-expressions">15.8.6. Path Expressions</a></li>
<li><a class="reference internal" href="#clauses">15.8.7. Clauses</a></li>
<li><a class="reference internal" href="#items">15.8.8. Items</a></li>
<li><a class="reference internal" href="#from-join-and-index-by">15.8.9. From, Join and Index by</a></li>
<li><a class="reference internal" href="#select-expressions">15.8.10. Select Expressions</a></li>
<li><a class="reference internal" href="#conditional-expressions">15.8.11. Conditional Expressions</a></li>
<li><a class="reference internal" href="#collection-expressions">15.8.12. Collection Expressions</a></li>
<li><a class="reference internal" href="#literal-values">15.8.13. Literal Values</a></li>
<li><a class="reference internal" href="#input-parameter">15.8.14. Input Parameter</a></li>
<li><a class="reference internal" href="#arithmetic-expressions">15.8.15. Arithmetic Expressions</a></li>
<li><a class="reference internal" href="#scalar-and-type-expressions">15.8.16. Scalar and Type Expressions</a></li>
<li><a class="reference internal" href="#aggregate-expressions">15.8.17. Aggregate Expressions</a></li>
<li><a class="reference internal" href="#case-expressions">15.8.18. Case Expressions</a></li>
<li><a class="reference internal" href="#id2">15.8.19. Other Expressions</a></li>
<li><a class="reference internal" href="#id3">15.8.20. Functions</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="batch-processing.html"
title="previous chapter">14. Batch Processing</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="query-builder.html"
title="next chapter">16. The QueryBuilder</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/dql-doctrine-query-language.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2010-12, Doctrine Project Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
<br/>
<a target="_BLANK" href="http://www.servergrove.com"><img src="../_static/servergrove.jpg" /></a> <br/><br/>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick" />
<input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
<input type="Submit" value="Donate via PayPal" />
</form>
</div>
</div>
<div id="bot-rcnr">
<div class="tl"><!-- corner --></div>
</div>
</div>
<a class="githublink" href="http://github.com/doctrine">Fork me on GitHub</a>
</body>
</html>
|