2016-02-10 125 views
0

我有以下的库:意想不到的结果

$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) 
    } 
} 
  1. 我期待随着年龄的增长为指标的数组,与值是与年份相匹配的对象数组,如果不是这样的话?
  2. db中有大约10条记录。一些如何只有这2出现。只输入两个不同年份的记录,这可能是只有两个记录的原因。有人知道为什么吗?

我该如何达到1的预期?

回答

0

默认情况下,原则给出对象。如果你想获得与所得到的数据的阵列可以使用

$result = $qb->getQuery()->execute(null, Query::HYDRATE_ARRAY); 

您需要包括学说\ ORM \查询这个,或者你只是使用

$result = $qb->getQuery()->getArrayResult(); 

的原因,你只能得到有关数据那两年是

->groupBy('r.year'); 

它消除了重复的条目。没有它,你会得到他们。

+0

感谢您的关注,但它并没有让我接近。通过删除groupBy,本练习的目标将被省略。另外,为了更加正确,默认情况下,Doctrine用对象给出了一个ArrayCollection。 – apfz