Jubz
Jubz

Reputation: 167

Doctrine2 query not populating entity object

I'm probably overlooking something, but I fail to understand why I am not getting any results from a Doctrine2 DQL. It does not fail, but it returns empty objects. The Symfony2 parameters are set correctly and I know that the database is being queried with the appropriate SQL (by logging SQL queries on MySQL).

The controller with the code not working:

public function debugAction() {
    $em = $this->getDoctrine()->getEntityManager();
    $qb = $em->createQueryBuilder();
    $account = $qb->select('a')
                   ->from('SkiviBundle:Account', 'a')
                   ->where('a.id = :id')
                   ->setParameter('id', 5)
                   ->getQuery()->getResult();
    return new \Symfony\Component\HttpFoundation\JsonResponse(array(
        'dql' => $qb->getDql(),
        'sql' => $qb->getQuery()->getSQL(),
        'params' => $qb->getQuery()->getParameters(),
        'account' => $account
    ));
}

Here is the JSON response I get, showing that the returned object is empty. I do not even get parameters back.

{
   "dql":"SELECT a FROM SkiviBundle:Account a WHERE a.id = :id",
   "sql":"SELECT a0_.id AS id0, a0_.business AS business1 FROM account a0_ WHERE a0_.id = ?",
   "params":{},
   "account":[{}]
}

I know that the SQL is executed successfully against the database. How do I make Doctrine2 populate the return object ($accounts) as an entity object? My Symfony2 application is standard (generated without further configurations, except add entities and controllers, and update parameters for database access.

Upvotes: 3

Views: 566

Answers (1)

yanyabo111
yanyabo111

Reputation: 285

seems can't use the :id in the dql, id is the key word? rename and try again

after all, here's a way to do this query:

$em->getRepository('SkiviBundle:Account')->find($id);

Upvotes: 1

Related Questions