Doctrine queryBuilder: return object not array
Asked Answered
G

4

5

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();
Gaffe answered 4/7, 2017 at 15:3 Comment(0)
U
3

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.

Upshaw answered 2/3, 2018 at 11:37 Comment(1)
Thanks for saving my day. I was stuck because of the select statement as it was returning an array of array instead of an array of entity objects.Thermy
C
5

you can use this:

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

Or this:

return $qb->getQuery()->getArrayResult();
Concerning answered 4/7, 2017 at 15:4 Comment(0)
U
3

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.

Upshaw answered 2/3, 2018 at 11:37 Comment(1)
Thanks for saving my day. I was stuck because of the select statement as it was returning an array of array instead of an array of entity objects.Thermy
C
0

if you want array of objects you have to set relation betwen Entities, and create a query by the owning side of relation.

example:

Tourney entity , Invite entity

Invite
    /**
     * @ORM\ManyToOne(targetEntity="Tourney", inversedBy="invites")
     */
    protected $tourneys;

Tourney
    /**

     * @ORM\OneToMany(targetEntity="Invite", mappedBy="tourneys", cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
     */
    protected $invites;

now you have to make query to the owning side of relation (Invite) and it will be holding all your join object data with Tourneys in field $invites

and it gives you array of objects. based on your query

remeber of setter $invites as setInvites(Tourney $invites) and by inverse side of relation setTourneys(Invite $tourneys)

Colporteur answered 2/3, 2018 at 11:21 Comment(0)
T
-2

Just add \Doctrine\ORM\Query::HYDRATE_ARRAY on getResult() like this

$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(\Doctrine\ORM\Query::HYDRATE_ARRAY);
Technique answered 4/7, 2017 at 15:6 Comment(1)
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.Interlope

© 2022 - 2024 — McMap. All rights reserved.