我在 mysql 中有一个列,我需要在我的 symfony 应用程序的列中检索表中总记录的大小。
这是表的结构,我想在其中获取同意列中记录的总大小,因此在本例中它应该返回 2:
id | agrees | disjoint |
1 | 1 | 0
2 | 1 | null
下面是我的方法,但它只是从表中获取所有内容,但我无法计算同意列的大小
$restresults = $this->getDoctrine()->getRepository('xxxBundle:Entity')->findAll();
$data = array();
foreach ($restresults as $restresult) {
array_push($data, $this->serialize($restresult));
}
$response = new Response(json_encode($data), 200);
$response->headers->set('Content-Type', 'application/json');
请问如何获取列中记录的总大小?
最佳答案
如果您需要计算表中的元素,请使用:
$qb = $entityManager->createQueryBuilder();
$qb->select('count(e.id)');
$qb->from('xxxBundle:Entity','e');
$count = $qb->getQuery()->getSingleScalarResult();
如果您需要总和列,请使用
$qb->select('sum(e.agrees)');
更多信息请参见 http://docs.doctrine-project.org/en/latest/reference/query-builder.html
关于php - 获取表中跨列的记录的总大小 - symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530492/