@@ -722,12 +722,39 @@ instead of querying for rows on a table (e.g. ``product``).
722
722
When querying in Doctrine, you have two options: writing pure Doctrine queries
723
723
or using Doctrine's Query Builder.
724
724
725
- Querying for Objects Using Doctrine's Query Builder
726
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
725
+ Querying for Objects with DQL
726
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 ``QueryBuilder `` for this::
730
+ Doctrine's native SQL-like language called DQL to do query for this::
731
+
732
+ $em = $this->getDoctrine()->getManager();
733
+ $query = $em->createQuery(
734
+ 'SELECT p
735
+ FROM AppBundle:Product p
736
+ WHERE p.price > :price
737
+ ORDER BY p.price ASC'
738
+ )->setParameter('price', '19.99');
739
+
740
+ $products = $query->getResult();
741
+
742
+ If you're comfortable with SQL, then DQL should feel very natural. The biggest
743
+ difference is that you need to think in terms of "objects" instead of rows
744
+ 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
+
748
+ The DQL syntax is incredibly powerful, allowing you to easily join between
749
+ entities (the topic of :ref: `relations <book-doctrine-relations >` will be
750
+ covered later), group, etc. For more information, see the official
751
+ `Doctrine Query Language `_ documentation.
752
+
753
+ Querying for Objects Using Doctrine's Query Builder
754
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
755
+
756
+ Instead of writing a DQL string, you can alternatively use a helpful object called
757
+ the ``QueryBuilder `` to build that string for you::
731
758
732
759
$repository = $this->getDoctrine()
733
760
->getRepository('AppBundle:Product');
@@ -759,33 +786,6 @@ is no result) or ``getOneOrNullResult()``::
759
786
For more information on Doctrine's Query Builder, consult Doctrine's
760
787
`Query Builder `_ documentation.
761
788
762
- Querying for Objects with DQL
763
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
764
-
765
- Instead of using the ``QueryBuilder ``, you can alternatively write the queries
766
- directly using DQL::
767
-
768
- $em = $this->getDoctrine()->getManager();
769
- $query = $em->createQuery(
770
- 'SELECT p
771
- FROM AppBundle:Product p
772
- WHERE p.price > :price
773
- ORDER BY p.price ASC'
774
- )->setParameter('price', '19.99');
775
-
776
- $products = $query->getResult();
777
-
778
- If you're comfortable with SQL, then DQL should feel very natural. The biggest
779
- difference is that you need to think in terms of "objects" instead of rows
780
- in a database. For this reason, you select *from * the ``AppBundle:Product ``
781
- *object * and then alias it as ``p `` (as you see, this is equal to what you
782
- already did in the previous section).
783
-
784
- The DQL syntax is incredibly powerful, allowing you to easily join between
785
- entities (the topic of :ref: `relations <book-doctrine-relations >` will be
786
- covered later), group, etc. For more information, see the official
787
- `Doctrine Query Language `_ documentation.
788
-
789
789
.. _book-doctrine-custom-repository-classes :
790
790
791
791
Custom Repository Classes
0 commit comments