Skip to content

Commit e9e012a

Browse files
authored
Merge pull request #7880 from kuraobi/update-doc-dql-qb
Update documentation to recommend DQL over QueryBuilder when possible
2 parents 9fef4e8 + d1db065 commit e9e012a

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

docs/en/reference/faq.rst

+15
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ No, it is not supported to sort by function in DQL. If you need this functionali
198198
use a native-query or come up with another solution. As a side note: Sorting with ORDER BY RAND() is painfully slow
199199
starting with 1000 rows.
200200

201+
Is it better to write DQL or to generate it with the query builder?
202+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203+
204+
The purpose of the ``QueryBuilder`` is to generate DQL dynamically,
205+
which is useful when you have optional filters, conditional joins, etc.
206+
207+
But the ``QueryBuilder`` is not an alternative to DQL, it actually generates DQL
208+
queries at runtime, which are then interpreted by Doctrine. This means that
209+
using the ``QueryBuilder`` to build and run a query is actually always slower
210+
than only running the corresponding DQL query.
211+
212+
So if you only need to generate a query and bind parameters to it,
213+
you should use plain DQL, as this is a simpler and much more readable solution.
214+
You should only use the ``QueryBuilder`` when you can't achieve what you want to do with a DQL query.
215+
201216
A Query fails, how can I debug it?
202217
----------------------------------
203218

docs/en/reference/query-builder.rst

+10-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ programmatically build queries, and also provides a fluent API.
99
This means that you can change between one methodology to the other
1010
as you want, or just pick a preferred one.
1111

12+
.. note::
13+
14+
The ``QueryBuilder`` is not an abstraction of DQL, but merely a tool to dynamically build it.
15+
You should still use plain DQL when you can, as it is simpler and more readable.
16+
More about this in the :doc:`FAQ <faq>`_.
17+
1218
Constructing a new QueryBuilder object
1319
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1420

@@ -80,7 +86,7 @@ Working with QueryBuilder
8086
High level API methods
8187
^^^^^^^^^^^^^^^^^^^^^^
8288

83-
To simplify even more the way you build a query in Doctrine, you can take
89+
The most straightforward way to build a dynamic query with the ``QueryBuilder`` is by taking
8490
advantage of Helper methods. For all base code, there is a set of
8591
useful methods to simplify a programmer's life. To illustrate how
8692
to work with them, here is the same example 6 re-written using
@@ -97,10 +103,9 @@ to work with them, here is the same example 6 re-written using
97103
->orderBy('u.name', 'ASC');
98104
99105
``QueryBuilder`` helper methods are considered the standard way to
100-
build DQL queries. Although it is supported, using string-based
101-
queries should be avoided. You are greatly encouraged to use
102-
``$qb->expr()->*`` methods. Here is a converted example 8 to
103-
suggested standard way to build queries:
106+
use the ``QueryBuilder``. The ``$qb->expr()->*`` methods can help you
107+
build conditional expressions dynamically. Here is a converted example 8 to
108+
suggested way to build queries with dynamic conditions:
104109

105110
.. code-block:: php
106111

docs/en/reference/working-with-objects.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,9 @@ DQL and its syntax as well as the Doctrine class can be found in
800800
:doc:`the dedicated chapter <dql-doctrine-query-language>`.
801801
For programmatically building up queries based on conditions that
802802
are only known at runtime, Doctrine provides the special
803-
``Doctrine\ORM\QueryBuilder`` class. More information on
803+
``Doctrine\ORM\QueryBuilder`` class. While this a powerful tool,
804+
it also brings more complexity to your code compared to plain DQL,
805+
so you should only use it when you need it. More information on
804806
constructing queries with a QueryBuilder can be found
805807
:doc:`in Query Builder chapter <query-builder>`.
806808

docs/en/tutorials/getting-started.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,7 @@ The console output of this script is then:
12101210
throw your ORM into the dumpster, because it doesn't support some
12111211
the more powerful SQL concepts.
12121212

1213-
1214-
Instead of handwriting DQL you can use the ``QueryBuilder`` retrieved
1213+
If you need to build your query dynamically, you can use the ``QueryBuilder`` retrieved
12151214
by calling ``$entityManager->createQueryBuilder()``. There are more
12161215
details about this in the relevant part of the documentation.
12171216

0 commit comments

Comments
 (0)