From a47d9771e259eb105139a6eaa83321a2fea0f58e Mon Sep 17 00:00:00 2001 From: "Alfonso M." Date: Sat, 23 May 2015 18:12:38 +0200 Subject: [PATCH 1/2] Place DQL in front of QueryBuilder --- book/doctrine.rst | 54 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/book/doctrine.rst b/book/doctrine.rst index 7ecd592ef63..02949c30bfd 100644 --- a/book/doctrine.rst +++ b/book/doctrine.rst @@ -722,6 +722,33 @@ 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 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. + Querying for Objects Using Doctrine's Query Builder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -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 From 027a835c52853005effb545f89184bcc3d10bea5 Mon Sep 17 00:00:00 2001 From: "Alfonso M." Date: Sat, 23 May 2015 19:24:33 +0200 Subject: [PATCH 2/2] Update doctrine.rst --- book/doctrine.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/book/doctrine.rst b/book/doctrine.rst index 02949c30bfd..2b61c0c0272 100644 --- a/book/doctrine.rst +++ b/book/doctrine.rst @@ -725,8 +725,9 @@ or using Doctrine's Query Builder. Querying for Objects with DQL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Instead of using the ``QueryBuilder``, you can alternatively write the queries -directly using 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 native SQL-like language called DQL to do query for this:: $em = $this->getDoctrine()->getManager(); $query = $em->createQuery( @@ -752,9 +753,8 @@ covered later), group, etc. For more information, see the official Querying for Objects Using Doctrine's Query Builder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -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:: +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');