Doctrine queryBuilder: return object not array

13,660

Solution 1

This isn't possible this way. In other words, you are doing it wrong.

You are telling Doctrine to return a collection of collections containing an entity and a string so this is what you get. Doctrine won't make an object out of that since it does not know how to hydrate such result.

[
  [entity, string],
  [entity, string],
  ....
]

If you wish to receive a collection of objects only, you would need to create a new entity that has both fields (related entity and a string property), then use a ResultSet mapping to hydrate that.

Solution 2

you can use this:

return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);

Or this:

return $qb->getQuery()->getArrayResult();
Share:
13,660
Keutelvocht
Author by

Keutelvocht

Bla bla bla

Updated on June 08, 2022

Comments

  • Keutelvocht
    Keutelvocht almost 2 years

    I have this query created with doctrine querybuilder, the return i get is an array of arrays. I would like to get a return that is an array of objects, is this possible?

    I know that normally Doctrine returns objects of an entity, bit since i have an inner join to get the name from another table it returns arrays.

    Thanks in advance.

       $qb->select('u', 'h.name')
            ->from('AppBundle:UserHose', 'u')
            ->innerJoin('AppBundle:Hose', 'h', 'WITH', 'u.hoseId = h.id')
            ->where('u.userId = :userId')
            ->orderBy('u.id', 'DESC')
                ->setParameter('userId', $userId); 
    
    
        return $qb->getQuery()->getResult();
    
  • Cerad
    Cerad almost 7 years
    Might want to read the question a bit closer. He is already getting an array result for some reason. He want an array of objects.