Skip to content

Commit 91a18cd

Browse files
committed
Changed order to QueryBuilder -> DQL
The QueryBuilder seems to be easier and more recommend to use. This meant the QueryBuilder section now contains the primary information and the DQL section is now reduced to somewhat a "side topic".
1 parent 7acc0d2 commit 91a18cd

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

book/doctrine.rst

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,42 @@ instead of querying for rows on a table (e.g. ``product``).
720720
When querying in Doctrine, you have two options: writing pure Doctrine queries
721721
or using Doctrine's Query Builder.
722722

723+
Querying for Objects Using Doctrine's Query Builder
724+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
725+
726+
Imagine that you want to query for products, but only return products that
727+
cost more than ``19.99``, ordered from cheapest to most expensive. You can use
728+
Doctrine's ``QueryBuilder`` for this::
729+
730+
$repository = $this->getDoctrine()
731+
->getRepository('AcmeStoreBundle:Product');
732+
733+
$query = $repository->createQueryBuilder('p')
734+
->where('p.price > :price')
735+
->setParameter('price', '19.99')
736+
->orderBy('p.price', 'ASC')
737+
->getQuery();
738+
739+
$products = $query->getResult();
740+
741+
The ``QueryBuilder`` object contains every method necessary to build your
742+
query. By calling the ``getQuery()`` method, the query builder returns a
743+
normal ``Query`` object, which can be used to get the result of the query.
744+
745+
The ``getResult()`` method returns an array of results. To get only one
746+
result, you can use ``getSingleResult()`` (which throws exception there is no
747+
result) or ``getOneOrNullResult()``::
748+
749+
$product = $query->getOneOrNullResult();
750+
751+
For more information on Doctrine's Query Builder, consult Doctrine's
752+
`Query Builder`_ documentation.
753+
723754
Querying for Objects with DQL
724755
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
725756

726-
Imagine that you want to query for products, but only return products that
727-
cost more than ``19.99``, ordered from cheapest to most expensive. From inside
728-
a controller, do the following::
757+
Instead of using the ``QueryBuilder``, you can alternatively write the queries
758+
directly using DQL::
729759

730760
$em = $this->getDoctrine()->getManager();
731761
$query = $em->createQuery(
@@ -737,49 +767,17 @@ a controller, do the following::
737767

738768
$products = $query->getResult();
739769

740-
The ``getResult()`` method returns an array of results. To get only one
741-
result, you can use ``getSingleResult()`` (which throws exception there is no
742-
result) or ``getOneOrNullResult()``::
743-
744-
$product = $query->getOneOrNullResult();
745-
746770
If you're comfortable with SQL, then DQL should feel very natural. The biggest
747771
difference is that you need to think in terms of "objects" instead of rows
748772
in a database. For this reason, you select *from* the ``AcmeStoreBundle:Product``
749-
*object* and then alias it as ``p``.
773+
*object* and then alias it as ``p`` (as you see, this is equal to what you
774+
already did in the previous section).
750775

751776
The DQL syntax is incredibly powerful, allowing you to easily join between
752777
entities (the topic of :ref:`relations <book-doctrine-relations>` will be
753778
covered later), group, etc. For more information, see the official Doctrine
754779
`Doctrine Query Language`_ documentation.
755780

756-
Using Doctrine's Query Builder
757-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
758-
759-
Instead of writing the queries directly, you can alternatively use Doctrine's
760-
``QueryBuilder`` to do the same job using a nice, object-oriented interface.
761-
If you use an IDE, you can also take advantage of auto-completion as you
762-
type the method names. From inside a controller::
763-
764-
$repository = $this->getDoctrine()
765-
->getRepository('AcmeStoreBundle:Product');
766-
767-
$query = $repository->createQueryBuilder('p')
768-
->where('p.price > :price')
769-
->setParameter('price', '19.99')
770-
->orderBy('p.price', 'ASC')
771-
->getQuery();
772-
773-
$products = $query->getResult();
774-
775-
The ``QueryBuilder`` object contains every method necessary to build your
776-
query. By calling the ``getQuery()`` method, the query builder returns a
777-
normal ``Query`` object, which is the same object you built directly in the
778-
previous section.
779-
780-
For more information on Doctrine's Query Builder, consult Doctrine's
781-
`Query Builder`_ documentation.
782-
783781
Custom Repository Classes
784782
~~~~~~~~~~~~~~~~~~~~~~~~~
785783

0 commit comments

Comments
 (0)