diff --git a/book/doctrine.rst b/book/doctrine.rst index 7ecd592ef63..2b61c0c0272 100644 --- a/book/doctrine.rst +++ b/book/doctrine.rst @@ -722,12 +722,39 @@ instead of querying for rows on a table (e.g. ``product``). When querying in Doctrine, you have two options: writing pure Doctrine queries or using Doctrine's Query Builder. -Querying for Objects Using Doctrine's Query Builder -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Querying for Objects with DQL +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Imagine that you want to query for products, but only return products that cost more than ``19.99``, ordered from cheapest to most expensive. You can use -Doctrine's ``QueryBuilder`` for this:: +Doctrine's native SQL-like language called DQL to do query for this:: + + $em = $this->getDoctrine()->getManager(); + $query = $em->createQuery( + 'SELECT p + FROM AppBundle:Product p + WHERE p.price > :price + ORDER BY p.price ASC' + )->setParameter('price', '19.99'); + + $products = $query->getResult(); + +If you're comfortable with SQL, then DQL should feel very natural. The biggest +difference is that you need to think in terms of "objects" instead of rows +in a database. For this reason, you select *from* the ``AppBundle:Product`` +*object* and then alias it as ``p`` (as you see, this is equal to what you +already did in the previous section). + +The DQL syntax is incredibly powerful, allowing you to easily join between +entities (the topic of :ref:`relations ` will be +covered later), group, etc. For more information, see the official +`Doctrine Query Language`_ documentation. + +Querying for Objects Using Doctrine's Query Builder +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Instead of writing a DQL string, you can alternatively use a helpful object called +the ``QueryBuilder`` to build that string for you:: $repository = $this->getDoctrine() ->getRepository('AppBundle:Product'); @@ -759,33 +786,6 @@ is no result) or ``getOneOrNullResult()``:: For more information on Doctrine's Query Builder, consult Doctrine's `Query Builder`_ documentation. -Querying for Objects with DQL -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Instead of using the ``QueryBuilder``, you can alternatively write the queries -directly using DQL:: - - $em = $this->getDoctrine()->getManager(); - $query = $em->createQuery( - 'SELECT p - FROM AppBundle:Product p - WHERE p.price > :price - ORDER BY p.price ASC' - )->setParameter('price', '19.99'); - - $products = $query->getResult(); - -If you're comfortable with SQL, then DQL should feel very natural. The biggest -difference is that you need to think in terms of "objects" instead of rows -in a database. For this reason, you select *from* the ``AppBundle:Product`` -*object* and then alias it as ``p`` (as you see, this is equal to what you -already did in the previous section). - -The DQL syntax is incredibly powerful, allowing you to easily join between -entities (the topic of :ref:`relations ` will be -covered later), group, etc. For more information, see the official -`Doctrine Query Language`_ documentation. - .. _book-doctrine-custom-repository-classes: Custom Repository Classes pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy