Doctrine orm GroupBy中的意外结果

Doctrine orm GroupBy中的意外结果,doctrine-orm,symfony,Doctrine Orm,Symfony,我拥有以下存储库: $qb = $this->getEntityManager()->createQueryBuilder(); $qb ->select('r') ->from('MyBundle:Rating', 'r') ->groupBy('r.year'); $result = $qb->getQuery()->execute(); return $result; 在控制器中执行转储: array(2) {

我拥有以下存储库:

$qb = $this->getEntityManager()->createQueryBuilder();
$qb
    ->select('r')
    ->from('MyBundle:Rating', 'r')
    ->groupBy('r.year');

$result = $qb->getQuery()->execute();
return $result;
在控制器中执行转储:

array(2) {
    [0]=>
  object(My\Entity\Rating)#553 (5) {
  ["id":protected]=>
    int(2)
    ["index":protected]=>
    int(1)
    ["year":protected]=>
    int(2010)
    ["percentage":protected]=>
    float(2.5)
  }
  [1]=>
  object(My\Entity\Rating)#550 (5) {
  ["id":protected]=>
    int(1)
    ["index":protected]=>
    int(1)
    ["year":protected]=>
    int(2016)
    ["percentage":protected]=>
    float(0)
  }
}
  • 我期待一个以年份作为索引的数组,值是一个匹配年份的对象数组,不是吗
  • 数据库中大约有10条记录。有些人认为只有这两个会出现。只输入两个不同年份的记录,这可能是只有两个记录的原因。有人知道为什么吗

  • 如何实现1中的期望?

    原则默认提供对象。如果要获取包含结果数据的数组,可以使用

    $result = $qb->getQuery()->execute(null, Query::HYDRATE_ARRAY);
    
    您需要为此包含条令\ORM\Query,否则只需使用

    $result = $qb->getQuery()->getArrayResult();
    
    你只得到这两年数据的原因是

    ->groupBy('r.year');
    

    这样可以消除重复条目。如果没有它,您将获得所有这些信息。

    谢谢您的关注,但这并没有让我更接近它。通过删除groupBy,此练习的目标将被忽略。此外,更准确地说,默认情况下,条令给出了一个包含对象的ArrayCollection。