Skip to content

Commit ce9456c

Browse files
herewegooweaverryan
authored andcommitted
Place DQL in front of QueryBuilder
1 parent 9fb8966 commit ce9456c

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

book/doctrine.rst

+27-27
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,33 @@ 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 with DQL
726+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727+
728+
Instead of using the ``QueryBuilder``, you can alternatively write the queries
729+
directly using DQL::
730+
731+
$em = $this->getDoctrine()->getManager();
732+
$query = $em->createQuery(
733+
'SELECT p
734+
FROM AppBundle:Product p
735+
WHERE p.price > :price
736+
ORDER BY p.price ASC'
737+
)->setParameter('price', '19.99');
738+
739+
$products = $query->getResult();
740+
741+
If you're comfortable with SQL, then DQL should feel very natural. The biggest
742+
difference is that you need to think in terms of "objects" instead of rows
743+
in a database. For this reason, you select *from* the ``AppBundle:Product``
744+
*object* and then alias it as ``p`` (as you see, this is equal to what you
745+
already did in the previous section).
746+
747+
The DQL syntax is incredibly powerful, allowing you to easily join between
748+
entities (the topic of :ref:`relations <book-doctrine-relations>` will be
749+
covered later), group, etc. For more information, see the official
750+
`Doctrine Query Language`_ documentation.
751+
725752
Querying for Objects Using Doctrine's Query Builder
726753
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727754

@@ -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)