Skip to content

Commit 7e7020d

Browse files
committed
[#5302] Re-reading sections after moving them, and tweaking some things that did not make sense anymore
Also, adding some extra code blocks to show how to query for one result
1 parent 3bec65d commit 7e7020d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

book/doctrine.rst

+20-15
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ Querying for Objects with DQL
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 native SQL-like language called DQL to do query for this::
730+
Doctrine's native SQL-like language called DQL to make a query for this::
731731

732732
$em = $this->getDoctrine()->getManager();
733733
$query = $em->createQuery(
@@ -738,12 +738,25 @@ Doctrine's native SQL-like language called DQL to do query for this::
738738
)->setParameter('price', '19.99');
739739

740740
$products = $query->getResult();
741+
// to get just one result:
742+
// $product = $query->setMaxResults(1)->getOneOrNullResult();
741743

742744
If you're comfortable with SQL, then DQL should feel very natural. The biggest
743745
difference is that you need to think in terms of "objects" instead of rows
744746
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+
*object* (an optional shortcut for ``AppBundle\Entity\Product``) and then
748+
alias it as ``p``.
749+
750+
.. tip::
751+
752+
Take note of the ``setParameter()`` method. When working with Doctrine,
753+
it's always a good idea to set any external values as "placeholders"
754+
(``:price`` in the example above) as it prevents SQL injection attacks.
755+
756+
The ``getResult()`` method returns an array of results. To get only one
757+
result, you can use ``getOneOrNullResult()``::
758+
759+
$product = $query->setMaxResults(1)->getOneOrNullResult();
747760

748761
The DQL syntax is incredibly powerful, allowing you to easily join between
749762
entities (the topic of :ref:`relations <book-doctrine-relations>` will be
@@ -759,30 +772,22 @@ the ``QueryBuilder`` to build that string for you::
759772
$repository = $this->getDoctrine()
760773
->getRepository('AppBundle:Product');
761774

775+
// createQueryBuilder automatically selects FROM AppBundle:Product
776+
// and aliases it to "p"
762777
$query = $repository->createQueryBuilder('p')
763778
->where('p.price > :price')
764779
->setParameter('price', '19.99')
765780
->orderBy('p.price', 'ASC')
766781
->getQuery();
767782

768783
$products = $query->getResult();
784+
// to get just one result:
785+
// $product = $query->setMaxResults(1)->getOneOrNullResult();
769786

770787
The ``QueryBuilder`` object contains every method necessary to build your
771788
query. By calling the ``getQuery()`` method, the query builder returns a
772789
normal ``Query`` object, which can be used to get the result of the query.
773790

774-
.. tip::
775-
776-
Take note of the ``setParameter()`` method. When working with Doctrine,
777-
it's always a good idea to set any external values as "placeholders"
778-
(``:price`` in the example above) as it prevents SQL injection attacks.
779-
780-
The ``getResult()`` method returns an array of results. To get only one
781-
result, you can use ``getSingleResult()`` (which throws an exception if there
782-
is no result) or ``getOneOrNullResult()``::
783-
784-
$product = $query->getOneOrNullResult();
785-
786791
For more information on Doctrine's Query Builder, consult Doctrine's
787792
`Query Builder`_ documentation.
788793

0 commit comments

Comments
 (0)