在查询构建器中索引

时间:2012-02-09 22:20:00

标签: php symfony1 doctrine query-builder

我正在使用symfony 2.0.10构建一个站点,在我的控制器中我使用以下代码查询数据库表:

$query = $this->getDoctrine()->getRepository('MyBundle:MyTable')->createQueryBuilder('x')->getQuery();

$data = $query->getResult();

我会在数组中索引的结果不是通过顺序号(0到N)而是通过指定的字段。我看到我可以在CreateQuery方法中指定 INDEX BY 但是如何使用查询构建器执行此操作?在this问题中给出了答复,但用户没有使用存储库。

编辑1

$query = $this->getDoctrine()->getEntityManager()->createQueryBuilder()->select('x')->from('MyBundle:MyTable', 'x', 'x.myIndexField')->getQuery();

1 个答案:

答案 0 :(得分:1)

我没试过,但我确定方法join有签名:

public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null)

最后一个参数是$indexBy,所以我想这就是你要找的东西;)