php - 计算 imagefk 等于的记录总数?在交响乐中

标签 php mysql symfony

我在数据库中有一个表,我可以在其中计算表中 imagefk 等于给定参数的记录总数。这是表格的可能结构

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

使用下面返回评论的总数,其中 imagefk 等于给定的参数,它返回所有记录而不是三行

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();

我对上述查询的挑战是 where 子句不计算基于此参数的 2 条记录

相反,它计算 3 条记录。请问哪里出了问题。请协助

最佳答案

您需要将您的实体与图像实体结合起来,然后您可以在图像 ID 上添加 where 子句,例如

$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();

关于php - 计算 imagefk 等于的记录总数?在交响乐中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47548134/

相关文章:

javascript - jQuery:$.ajax() 重定向到其 URL 表单操作参数,而不是对表单操作执行 AJAX 请求

mysql - 如何将 BGL 转换为 MySQL 表?

php - 无法用php向mysql输入数据

php - Symfony2 AuthenticationCredentialsNotFoundException @Route

PHP : apc_store doesn't work as intended

php - 创建新的 MVC 文件失败

php - 组合来自不同表、具有不同字段名称的 2 个查询

mysql - 两个日期列之间的日期范围

javascript - 如何使用dropzone上传base64图片资源?

php - Laravel 上的凭证