How to get a one-dimensional scalar array as a doctrine dql query result?

How to get a one-dimensional scalar array as a doctrine dql query result?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Scalar Arrays from Doctrine DQL Queries

When you run a Doctrine query that selects a single column, such as SELECT id FROM auction, the ORM wraps each result in an associative array keyed by the column name. This behavior is consistent and predictable: calling getScalarResult() on the Query object returns something like ['id' => 1], ['id' => 2]. While this structure works perfectly for hydration into entities or accessing fields via keys, it often feels clunky when you simply need a flat list of integers. You want array(1, 2) rather than an array of arrays.

The core issue lies in how Doctrine handles result mapping. By default, getScalarResult() is designed to return a collection of scalar results, where each item represents a single row mapped to its column keys. To transform this into a one-dimensional array of values, you have two primary approaches depending on your specific use case and the version of Doctrine you are using.

The most straightforward method involves mapping the query result explicitly as an array before retrieval. You can achieve this by defining a DQL alias for the result column and then configuring the query to return an array of that scalar value. However, Doctrine does not have a built-in "flatten" option for getScalarResult(). Instead, you must manipulate the result set after retrieval or adjust the mapping strategy.

A common pattern is to fetch the results as an entity collection first, if possible, but in this case, we are querying a raw column. Since Doctrine lacks a direct method to return a flattened scalar array from getScalarResult(), the most robust solution often involves iterating over the result set and extracting the values manually. This approach ensures compatibility across different Doctrine versions and avoids relying on undocumented behaviors.

Here is how you can implement this in practice:

$ids = [];
$results = $em->createQuery('SELECT a.id FROM Auction a')->getResult();

foreach ($results as $result) {
    $ids[] = $result['id'];
}

While this loop seems verbose, it is explicit and safe. It guarantees that you are pulling the scalar value out of the associative array key 'id' regardless of how Doctrine internally structures the result row. This manual extraction avoids any potential issues with entity hydration or column name casing inconsistencies.

If you are using a modern version of Doctrine (2.x or 3.x), there is no magic method to bypass the associative wrapper in getScalarResult() because that is its contract. The ORM prioritizes type safety and clear mapping over convenience for raw data extraction. If performance becomes a concern with massive datasets, fetching into an array this way is generally efficient enough, as you are avoiding the overhead of entity hydration entirely.

Another consideration is whether you actually need Doctrine for this specific query. If your goal is simply to retrieve IDs without joining or filtering by complex entity state, using $entityManager->getConnection()->executeQuery() with a raw SQL SELECT id might yield a result set that is easier to iterate into a flat array depending on your driver configuration. However, mixing raw SQL and Doctrine DQL requires careful attention to connection management and transaction boundaries.

In summary, the discrepancy between your desired output and what getScalarResult() provides is by design. The method returns associative arrays to preserve column metadata. To get a one-dimensional scalar array, you must extract the values from the result set manually or iterate through them. This ensures your application logic remains decoupled from Doctrine's internal representation of query results.

Our network