dsizd368332 2017-11-29 08:25
浏览 6
已采纳

计算imagefk等于的记录总数? 在symfony

I have a table in the db where I can count the total number of records in the table where imagefk is equal to the parameter given. Here is a possible structure of the table

id | reviews | bookname | imagefk
1  |  1      | xyz      | null
2  |  1      | xyz      | 53
3  |  1      | xyz      | 53

Using below to return the total count of reviews where imagefk is equal to the parameter given, it returns all records instead of the three rows

public function getAllImagesAction(String imagefk)
    {
    $entityManager = $this->getDoctrine()->getManager();
            $qb = $entityManager->createQueryBuilder();
            $qb->select('count(e.reviews)');
            $qb->from('xxxBundle:Books','e')
                    ->add('where', 'e.imagefk = imagefk'); // making the parameter dynamic and to pull from the url routing.yml file
            $count = $qb->getQuery()->getSingleScalarResult();

my challenge with the above query is that the where clause does not count 2 records based on this parameter

instead it counts 3 records. Please what could be wrong. Kindly assist

  • 写回答

1条回答 默认 最新

  • douwei4370 2017-11-29 09:10
    关注

    You need to join your entity with Images entity and then you can add where clause on image id like

    $entityManager = $this->getDoctrine()->getManager();
        $qb = $entityManager->createQueryBuilder();
        $qb->select('count(b.reviews)')
           ->from('xxxBundle:Books','b')
           ->join('b.imagerating','i')
           ->where('i.id = :id')
           ->setParameter('id', 53);       
    $count = $qb->getQuery()->getSingleScalarResult();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容