Skip to content

Commit

Permalink
[Documentation] Query Result Formats
Browse files Browse the repository at this point in the history
Page: https://www.doctrine-project.org/projects/doctrine-orm/en/2.19/reference/dql-doctrine-query-language.html#query-result-formats

Follow-up of #11359

The table I suggested is probably not working, since the text for each method is too long. And what I really wanted is to make it more *scanable*. So I tried boldfacing - if this doesn't work, I'll try something else.

Questions:

1. This section here is basically the same as https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/dql-doctrine-query-language.html#hydration-modes ! So I'll try to merge them (in another PR), OK? I think the list is a better format (more scanable) - since those methods all work the same, there's no need for a full-blown code sample for each, IMO.

2. `getSingleColumnResult()` is missing.
  • Loading branch information
ThomasLandauer committed Mar 17, 2024
1 parent abcad6f commit ae84225
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions docs/en/reference/dql-doctrine-query-language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ The Query class
---------------

An instance of the ``Doctrine\ORM\Query`` class represents a DQL
query. You create a Query instance be calling
query. You create a Query instance by calling
``EntityManager#createQuery($dql)``, passing the DQL query string.
Alternatively you can create an empty ``Query`` instance and invoke
``Query#setDQL($dql)`` afterwards. Here are some examples:
Expand All @@ -1023,55 +1023,39 @@ Alternatively you can create an empty ``Query`` instance and invoke
Query Result Formats
~~~~~~~~~~~~~~~~~~~~

The format in which the result of a DQL SELECT query is returned
can be influenced by a so-called ``hydration mode``. A hydration
mode specifies a particular way in which a SQL result set is
transformed. Each hydration mode has its own dedicated method on
the Query class. Here they are:
The way in which the SQL result set of a DQL SELECT query is transformed
to PHP is determined by the so-called "hydration mode":


- ``Query#getResult()``: Retrieves a collection of objects. The
- **``getResult()``** (``HYDRATE_OBJECT``): Retrieves a collection of objects. The
result is either a plain collection of objects (pure) or an array
where the objects are nested in the result rows (mixed).
- ``Query#getSingleResult()``: Retrieves a single object. If the
result contains more than one object, an ``NonUniqueResultException``
is thrown. If the result contains no objects, an ``NoResultException``
- **``getSingleResult()``**: Retrieves a single object. If the
result contains more than one object, a ``NonUniqueResultException``
is thrown. If the result contains no objects, a ``NoResultException``
is thrown. The pure/mixed distinction does not apply.
- ``Query#getOneOrNullResult()``: Retrieve a single object. If the
- **``getOneOrNullResult()``**: Retrieve a single object. If the
result contains more than one object, a ``NonUniqueResultException``
is thrown. If no object is found null will be returned.
- ``Query#getArrayResult()``: Retrieves an array graph (a nested
array) that is largely interchangeable with the object graph
generated by ``Query#getResult()`` for read-only purposes.
- **``getArrayResult()``** (``HYDRATE_ARRAY``): Retrieves an array graph (a nested
array) for read-only purposes.

.. note::

An array graph can differ from the corresponding object
graph in certain scenarios due to the difference of the identity
semantics between arrays and objects.



- ``Query#getScalarResult()``: Retrieves a flat/rectangular result
- **``getScalarResult()``** (``HYDRATE_SCALAR``): Retrieves a flat/rectangular result
set of scalar values that can contain duplicate data. The
pure/mixed distinction does not apply.
- ``Query#getSingleScalarResult()``: Retrieves a single scalar
- **``getSingleScalarResult()``** (``HYDRATE_SINGLE_SCALAR``: Retrieves a single scalar
value from the result returned by the dbms. If the result contains
more than a single scalar value, an exception is thrown. The
pure/mixed distinction does not apply.

Instead of using these methods, you can alternatively use the
general-purpose method
``Query#execute(array $params = [], $hydrationMode = Query::HYDRATE_OBJECT)``.
Using this method you can directly supply the hydration mode as the
second parameter via one of the Query constants. In fact, the
methods mentioned earlier are just convenient shortcuts for the
execute method. For example, the method ``Query#getResult()``
internally invokes execute, passing in ``Query::HYDRATE_OBJECT`` as
the hydration mode.

The use of the methods mentioned earlier is generally preferred as
it leads to more concise code.
In parentheses are the constants of the ``Query`` class which you can use with the
general-purpose method ``Query::execute(array $params = [], $hydrationMode = Query::HYDRATE_OBJECT)``.
In fact, the recommended methods in the list are just convenient shortcuts for the hydration mode.

Pure and Mixed Results
~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit ae84225

Please sign in to comment.