Symfony2 Select one column in doctrine

帅比萌擦擦* 提交于 2019-12-01 05:14:24

Since you are requesting single column of each record you are bound to expect an array. That being said you should replace getResult with getArrayResult() because you can't enforce object hydration:

$data = $qb->getArrayResult();
Now, you have structure:
    $data[0]['address']
    $data[1]['address']
    ....

Hope this helps.

As for the discussion about performance in comments I generally agree with you for not wanting all 30 column fetch every time. However, in that case, you should consider writing named queries in order to minimize impact if you database ever gets altered.

You can use partial objects to only hydrate one field and still return a object.

This worked for me:

  $qb = $repository->createQueryBuilder('i')
    ->select('i.name')
    ->...

Use partial objects like this to select fields

$qb = $this->createQueryBuilder('r')
    ->select(array('partial r.{id,address}'))
    ...

Put your field names between the brackets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!