Skip to content

Commit 3bec65d

Browse files
committed
minor #5302 Place DQL in front of QueryBuilder (alfonsomga)
This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes #5302). Discussion ---------- Place DQL in front of QueryBuilder | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.7 | Fixed tickets | #5022 Commits ------- 2335206 Update doctrine.rst ce9456c Place DQL in front of QueryBuilder
2 parents 9fb8966 + 2335206 commit 3bec65d

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

Diff for: book/doctrine.rst

+30-30
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,39 @@ instead of querying for rows on a table (e.g. ``product``).
722722
When querying in Doctrine, you have two options: writing pure Doctrine queries
723723
or using Doctrine's Query Builder.
724724

725-
Querying for Objects Using Doctrine's Query Builder
726-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
725+
Querying for Objects with DQL
726+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727727

728728
Imagine that you want to query for products, but only return products that
729729
cost more than ``19.99``, ordered from cheapest to most expensive. You can use
730-
Doctrine's ``QueryBuilder`` for this::
730+
Doctrine's native SQL-like language called DQL to do query for this::
731+
732+
$em = $this->getDoctrine()->getManager();
733+
$query = $em->createQuery(
734+
'SELECT p
735+
FROM AppBundle:Product p
736+
WHERE p.price > :price
737+
ORDER BY p.price ASC'
738+
)->setParameter('price', '19.99');
739+
740+
$products = $query->getResult();
741+
742+
If you're comfortable with SQL, then DQL should feel very natural. The biggest
743+
difference is that you need to think in terms of "objects" instead of rows
744+
in a database. For this reason, you select *from* the ``AppBundle:Product``
745+
*object* and then alias it as ``p`` (as you see, this is equal to what you
746+
already did in the previous section).
747+
748+
The DQL syntax is incredibly powerful, allowing you to easily join between
749+
entities (the topic of :ref:`relations <book-doctrine-relations>` will be
750+
covered later), group, etc. For more information, see the official
751+
`Doctrine Query Language`_ documentation.
752+
753+
Querying for Objects Using Doctrine's Query Builder
754+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
755+
756+
Instead of writing a DQL string, you can alternatively use a helpful object called
757+
the ``QueryBuilder`` to build that string for you::
731758

732759
$repository = $this->getDoctrine()
733760
->getRepository('AppBundle:Product');
@@ -759,33 +786,6 @@ is no result) or ``getOneOrNullResult()``::
759786
For more information on Doctrine's Query Builder, consult Doctrine's
760787
`Query Builder`_ documentation.
761788

762-
Querying for Objects with DQL
763-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
764-
765-
Instead of using the ``QueryBuilder``, you can alternatively write the queries
766-
directly using DQL::
767-
768-
$em = $this->getDoctrine()->getManager();
769-
$query = $em->createQuery(
770-
'SELECT p
771-
FROM AppBundle:Product p
772-
WHERE p.price > :price
773-
ORDER BY p.price ASC'
774-
)->setParameter('price', '19.99');
775-
776-
$products = $query->getResult();
777-
778-
If you're comfortable with SQL, then DQL should feel very natural. The biggest
779-
difference is that you need to think in terms of "objects" instead of rows
780-
in a database. For this reason, you select *from* the ``AppBundle:Product``
781-
*object* and then alias it as ``p`` (as you see, this is equal to what you
782-
already did in the previous section).
783-
784-
The DQL syntax is incredibly powerful, allowing you to easily join between
785-
entities (the topic of :ref:`relations <book-doctrine-relations>` will be
786-
covered later), group, etc. For more information, see the official
787-
`Doctrine Query Language`_ documentation.
788-
789789
.. _book-doctrine-custom-repository-classes:
790790

791791
Custom Repository Classes

0 commit comments

Comments
 (0)