2016-09-23 153 views
1

我在Symfony 3中编写了一个控制台应用程序,但由于某种原因,我的查询结果只返回了1个结果,我在数组中确认了它的运行结果并返回了几个结果。下面是相关代码:为什么getResult()只返回一个只有最后一个记录的数组?

$arr_id = array(); 
$em = $this->getContainer()->get('doctrine')->getManager(); 
$dql2 = 'SELECT r.productId as pid, COUNT(r.id) as cnt 
      FROM AppBundle:ReferralTrack r 
      WHERE r.productId in (:in) 
      GROUP BY r.productId 
      ORDER BY cnt DESC'; 
// $arr_id is populated prior to the next command and is, for example, [1,2,3,4,5] 
$rt = $em->createQuery($dql2) 
    ->setParameters(
     array(
      'in' => implode(',', $arr_id), 
     ) 
    ) 
; 
$traffic_count = $rt->getResult(); 

当我运行在MySQL中查询,通过RT-$所示的完全相同的查询> getSql()和更换?结合了来自破灭呼应了查询(“”,$ arr_id),我得到了类似的结果是这样的:

echo print_r($traffic_count, 1); 
--------------------------------- 
Array 
(
[0] => Array 
    (
     [pid] => 11234 
     [cnt] => 21 
    ) 

) 

这是我在MySQL运行查询:

SELECT r0_.product_id AS pid, COUNT(r0_.id) AS cnt 
FROM referral_track r0_ 
WHERE r0_.product_id 
IN (11234, 57, 58, 60, 61, 9677, 11216, 11217, 11239, 11296) 
GROUP BY r0_.product_id 
ORDER BY cnt DESC 

并返回7个结果。我注意到的是getResult()似乎总是返回最后一个记录,那就是在那里显示的数组。到底是怎么回事?

回答

1

尝试:

$rt = $em->createQuery($dql2) 
     ->setParameters(
      array(
       'in' => $arr_id, 
      ) 
     ) 
    ; 
+0

感谢@COil。我现在理解了很多教义,但不知道为什么它需要一个实际的数组,而不是你在MySQL中编写它的方式。 – pogeybait

相关问题