我在数据库中有一个表,我可以在其中计算表中 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/