【问题标题】:Why is getResult() only returning an array with only the last record?为什么 getResult() 只返回一个只有最后一条记录的数组?
【发布时间】:2016-09-23 08:45:59
【问题描述】:

我正在 Symfony 3 中编写一个控制台应用程序,但由于某种原因,我的查询结果返回数组中只有 1 个结果,我确认在 MySQL 中运行它并返回几个结果。以下是相关代码:

$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() 显示完全相同的查询并替换 ?在从 implode(',', $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() 似乎总是返回最后一条记录,这就是那里显示的数组。怎么回事?

【问题讨论】:

    标签: doctrine-orm symfony


    【解决方案1】:

    试试:

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

    【讨论】:

    • 感谢@COil。我现在了解了很多 Doctrine,但不确定为什么它需要一个实际的数组而不是你在 MySQL 中编写它的方式。
    猜你喜欢
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多