Skip to content

Commit

Permalink
Place DQL in front of QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
herewegoo authored and weaverryan committed May 25, 2015
1 parent 9fb8966 commit ce9456c
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions book/doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <book-doctrine-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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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 <book-doctrine-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
Expand Down

0 comments on commit ce9456c

Please sign in to comment.