Return array, not object from Doctrine query - Symfony2

Return array, not object from Doctrine query - Symfony2

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

Stefan Bogdanescu

Founder & Senior Architect

Return array, not object from Doctrine query - Symfony2

When working with Doctrine in Symfony 2, it is a common expectation that passing Query::HYDRATE_ARRAY to the findAll() method will transform the result set into a flat list of associative arrays. If you are accessing data via $result[4]['age'], expecting this syntax to work seamlessly after calling findAll(array(), Query::HYDRATE_ARRAY), you might encounter frustration because Doctrine still returns an array of entity objects rather than raw arrays.

The core issue lies in the distinction between what the hydration constant requests and how Doctrine constructs the result set. The Query::HYDRATE_ARRAY constant instructs Doctrine to populate the object properties of the entities retrieved from the database with scalar values, effectively simulating the behavior of a simple array access on individual records. However, this does not alter the fundamental structure of the collection returned by findAll(). The method signature dictates that it returns an iterable collection where each item corresponds to a mapped entity class defined in your entity file.

Consequently, even when hydration is set to Query::HYDRATE_ARRAY, the top-level container remains a collection of objects. Attempting to access $result[4]['age'] will fail because $result[4] is still an instance of your entity class, not an associative array. To achieve the desired flat structure where every field is directly accessible as a key, you must explicitly convert the collection after retrieval or use a different approach during the query execution.

The most robust solution involves iterating through the result set and converting each object into an array manually. This can be accomplished using json_decode(json_encode($entity), true) or by leveraging the Doctrine method toArray() if available on your specific entity configuration, though direct iteration is often clearer. Here is how you might implement this conversion:

$entities = $this->getDoctrine()
    ->getRepository('MyBundle:MyEntity')
    ->findAll(array(), Query::HYDRATE_ARRAY);

$result = array();
foreach ($entities as $entity) {
    // Convert the entity object to an associative array
    $result[] = (array)$entity;
}

Alternatively, if you prefer a functional approach without explicit loops, you can utilize array_map. This ensures that every item in the final $result array is a native PHP array, allowing you to safely execute $result[4]['age'].

It is important to note the trade-offs involved in this decision. While returning raw arrays simplifies data manipulation for external consumers or API responses, it breaks the type safety provided by Doctrine's object model. You lose immediate access to entity methods and magic getters unless they are explicitly defined as array-accessible properties. Furthermore, if your entities utilize lazy-loading proxies to handle relationships efficiently, converting them to arrays immediately might trigger unnecessary database queries or lock up connections if not handled carefully.

If you require a completely different structure where the entire dataset is flattened into a single associative array keyed by index, this manual conversion step is mandatory regardless of the hydration mode used in the query. Understanding that Query::HYDRATE_ARRAY affects internal property population rather than the collection wrapper itself prevents confusion when integrating Doctrine data with frontend logic that expects JSON-like structures. For more complex scenarios involving relationship mapping or custom serialization requirements, consider leveraging a dedicated serializer library to handle the transformation post-query, ensuring your application remains clean and maintainable.

Our network