@@ -720,12 +720,42 @@ instead of querying for rows on a table (e.g. ``product``).
720
720
When querying in Doctrine, you have two options: writing pure Doctrine queries
721
721
or using Doctrine's Query Builder.
722
722
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
+
723
754
Querying for Objects with DQL
724
755
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
725
756
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::
729
759
730
760
$em = $this->getDoctrine()->getManager();
731
761
$query = $em->createQuery(
@@ -737,49 +767,17 @@ a controller, do the following::
737
767
738
768
$products = $query->getResult();
739
769
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
-
746
770
If you're comfortable with SQL, then DQL should feel very natural. The biggest
747
771
difference is that you need to think in terms of "objects" instead of rows
748
772
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).
750
775
751
776
The DQL syntax is incredibly powerful, allowing you to easily join between
752
777
entities (the topic of :ref: `relations <book-doctrine-relations >` will be
753
778
covered later), group, etc. For more information, see the official Doctrine
754
779
`Doctrine Query Language `_ documentation.
755
780
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
-
783
781
Custom Repository Classes
784
782
~~~~~~~~~~~~~~~~~~~~~~~~~
785
783
0 commit comments