@@ -727,7 +727,7 @@ Querying for Objects with DQL
727
727
728
728
Imagine that you want to query for products, but only return products that
729
729
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::
731
731
732
732
$em = $this->getDoctrine()->getManager();
733
733
$query = $em->createQuery(
@@ -738,12 +738,25 @@ Doctrine's native SQL-like language called DQL to do query for this::
738
738
)->setParameter('price', '19.99');
739
739
740
740
$products = $query->getResult();
741
+ // to get just one result:
742
+ // $product = $query->setMaxResults(1)->getOneOrNullResult();
741
743
742
744
If you're comfortable with SQL, then DQL should feel very natural. The biggest
743
745
difference is that you need to think in terms of "objects" instead of rows
744
746
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();
747
760
748
761
The DQL syntax is incredibly powerful, allowing you to easily join between
749
762
entities (the topic of :ref: `relations <book-doctrine-relations >` will be
@@ -759,30 +772,22 @@ the ``QueryBuilder`` to build that string for you::
759
772
$repository = $this->getDoctrine()
760
773
->getRepository('AppBundle:Product');
761
774
775
+ // createQueryBuilder automatically selects FROM AppBundle:Product
776
+ // and aliases it to "p"
762
777
$query = $repository->createQueryBuilder('p')
763
778
->where('p.price > :price')
764
779
->setParameter('price', '19.99')
765
780
->orderBy('p.price', 'ASC')
766
781
->getQuery();
767
782
768
783
$products = $query->getResult();
784
+ // to get just one result:
785
+ // $product = $query->setMaxResults(1)->getOneOrNullResult();
769
786
770
787
The ``QueryBuilder `` object contains every method necessary to build your
771
788
query. By calling the ``getQuery() `` method, the query builder returns a
772
789
normal ``Query `` object, which can be used to get the result of the query.
773
790
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
-
786
791
For more information on Doctrine's Query Builder, consult Doctrine's
787
792
`Query Builder `_ documentation.
788
793
0 commit comments