@@ -722,27 +722,30 @@ 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 make a query for this::
731
731
732
- $repository = $this->getDoctrine()
733
- ->getRepository('AppBundle:Product');
734
-
735
- $query = $repository->createQueryBuilder('p')
736
- ->where('p.price > :price')
737
- ->setParameter('price', '19.99')
738
- ->orderBy('p.price', 'ASC')
739
- ->getQuery();
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');
740
739
741
740
$products = $query->getResult();
741
+ // to get just one result:
742
+ // $product = $query->setMaxResults(1)->getOneOrNullResult();
742
743
743
- The ``QueryBuilder `` object contains every method necessary to build your
744
- query. By calling the ``getQuery() `` method, the query builder returns a
745
- normal ``Query `` object, which can be used to get the result of the query.
744
+ If you're comfortable with SQL, then DQL should feel very natural. The biggest
745
+ difference is that you need to think in terms of "objects" instead of rows
746
+ in a database. For this reason, you select *from * the ``AppBundle:Product ``
747
+ *object * (an optional shortcut for ``AppBundle\Entity\Product ``) and then
748
+ alias it as ``p ``.
746
749
747
750
.. tip ::
748
751
@@ -751,40 +754,42 @@ normal ``Query`` object, which can be used to get the result of the query.
751
754
(``:price `` in the example above) as it prevents SQL injection attacks.
752
755
753
756
The ``getResult() `` method returns an array of results. To get only one
754
- result, you can use ``getSingleResult() `` (which throws an exception if there
755
- is no result) or ``getOneOrNullResult() ``::
757
+ result, you can use ``getOneOrNullResult() ``::
756
758
757
- $product = $query->getOneOrNullResult();
759
+ $product = $query->setMaxResults(1)-> getOneOrNullResult();
758
760
759
- For more information on Doctrine's Query Builder, consult Doctrine's
760
- `Query Builder `_ documentation.
761
+ The DQL syntax is incredibly powerful, allowing you to easily join between
762
+ entities (the topic of :ref: `relations <book-doctrine-relations >` will be
763
+ covered later), group, etc. For more information, see the official
764
+ `Doctrine Query Language `_ documentation.
761
765
762
- Querying for Objects with DQL
763
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
766
+ Querying for Objects Using Doctrine's Query Builder
767
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
764
768
765
- Instead of using the `` QueryBuilder `` , you can alternatively write the queries
766
- directly using DQL ::
769
+ Instead of writing a DQL string , you can alternatively use a helpful object called
770
+ the `` QueryBuilder `` to build that string for you ::
767
771
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');
772
+ $repository = $this->getDoctrine()
773
+ ->getRepository('AppBundle:Product');
774
+
775
+ // createQueryBuilder automatically selects FROM AppBundle:Product
776
+ // and aliases it to "p"
777
+ $query = $repository->createQueryBuilder('p')
778
+ ->where('p.price > :price')
779
+ ->setParameter('price', '19.99')
780
+ ->orderBy('p.price', 'ASC')
781
+ ->getQuery();
775
782
776
783
$products = $query->getResult();
784
+ // to get just one result:
785
+ // $product = $query->setMaxResults(1)->getOneOrNullResult();
777
786
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).
787
+ The ``QueryBuilder `` object contains every method necessary to build your
788
+ query. By calling the ``getQuery() `` method, the query builder returns a
789
+ normal ``Query `` object, which can be used to get the result of the query.
783
790
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.
791
+ For more information on Doctrine's Query Builder, consult Doctrine's
792
+ `Query Builder `_ documentation.
788
793
789
794
.. _book-doctrine-custom-repository-classes :
790
795
0 commit comments