How to return object from a DQL query?

Mr B picture Mr B · Aug 15, 2011 · Viewed 9.8k times · Source

I have written a DQL query in Doctrine 2:

$qb->select('r.position')
    ->from('\Entities\Races', 'r')
    ->where($qb->expr()->eq('r.entrantId', ':entrant_id'))
    ->setParameter('entrant_id', $this->entrantId);
$query = $qb->getQuery();
$aRaces = $query->getResult();

Currently it returns the results of the query in an array like so:

Array
(
    [0] => Array
        (
            [position] => 10
        )

    [1] => Array
        (
            [position] => 4
        )
)

I want the result to return an array of Races objects so that I can access the methods associated with the object (I'm pretty sure the previous version of Doctrine returned objects by default).

I tried:

$aRaces = $query->getResult(Query::HYDRATE_OBJECT);

But that didn't make a difference.

Appreciate the help

Answer

J0HN picture J0HN · Aug 15, 2011

You are fetching only position column from DB. Try replacing select('r.position') with select(r). See DQL reference

If you need objects with only position attribute, refer to partial objects