@@ -728,65 +728,18 @@ a controller, do the following::
728
728
729
729
$products = $query->getResult();
730
730
731
+ The ``getResult() `` method returns an array of results.
732
+
731
733
If you're comfortable with SQL, then DQL should feel very natural. The biggest
732
734
difference is that you need to think in terms of "objects" instead of rows
733
- in a database. For this reason, you select *from * ``AcmeStoreBundle:Product ``
734
- and then alias it as ``p ``.
735
-
736
- The ``getResult() `` method returns an array of results. If you're querying
737
- for just one object, you can use the ``getSingleResult() `` method instead::
738
-
739
- $product = $query->getSingleResult();
740
-
741
- .. caution ::
742
-
743
- The ``getSingleResult() `` method throws a ``Doctrine\ORM\NoResultException ``
744
- exception if no results are returned and a ``Doctrine\ORM\NonUniqueResultException ``
745
- if *more * than one result is returned. If you use this method, you may
746
- need to wrap it in a try-catch block and ensure that only one result is
747
- returned (if you're querying on something that could feasibly return
748
- more than one result)::
749
-
750
- $query = $em->createQuery('SELECT ...')
751
- ->setMaxResults(1);
752
-
753
- try {
754
- $product = $query->getSingleResult();
755
- } catch (\Doctrine\Orm\NoResultException $e) {
756
- $product = null;
757
- }
758
- // ...
735
+ in a database. For this reason, you select *from * the ``AcmeStoreBundle:Product ``
736
+ *object * and then alias it as ``p ``.
759
737
760
738
The DQL syntax is incredibly powerful, allowing you to easily join between
761
739
entities (the topic of :ref: `relations <book-doctrine-relations >` will be
762
740
covered later), group, etc. For more information, see the official Doctrine
763
741
`Doctrine Query Language `_ documentation.
764
742
765
- .. sidebar :: Setting Parameters
766
-
767
- Take note of the ``setParameter() `` method. When working with Doctrine,
768
- it's always a good idea to set any external values as "placeholders",
769
- which was done in the above query:
770
-
771
- .. code-block :: text
772
-
773
- ... WHERE p.price > :price ...
774
-
775
- You can then set the value of the ``price `` placeholder by calling the
776
- ``setParameter() `` method::
777
-
778
- ->setParameter('price', '19.99')
779
-
780
- Using parameters instead of placing values directly in the query string
781
- is done to prevent SQL injection attacks and should *always * be done.
782
- If you're using multiple parameters, you can set their values at once
783
- using the ``setParameters() `` method::
784
-
785
- ->setParameters(array(
786
- 'price' => '19.99',
787
- 'name' => 'Foo',
788
- ))
789
-
790
743
Using Doctrine's Query Builder
791
744
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
792
745
0 commit comments