如何在 DQL 查询中获取 MONTH 名称?
How can I get the MONTH name in a DQL query?
我有一个查询要获取按月分组的插入次数。我已经安装并运行了 beberlei Doctrine 扩展,但是数组 return 以月份数字作为键。如何在图表行中使用,我想显示月份名称而不是月份编号。
最好的方法是什么?
这个查询:
//get and format the current year
$date = new \DateTime();
$year = $date->format('Y');
//create query
$qb = $this->_em->createQueryBuilder();
$qb->select(['MONTH(p.createdAt) as month', 'count(ccp) as insertions'])
->from(CareConnectPatient::class, 'ccp')
->join('ccp.person', 'p')
->where('YEAR(p.createdAt) = :year')->setParameter('year', $year);
$qb->groupBy('month');
return $qb->getQuery()->getScalarResult();
然后我这样做了,结果是:
`$countMonths = count($cCPatientGroupedByMonth);
for ($count = 0; $count<$countMonths; $count++){
$arrayKeyAsMonth[$cCPatientGroupedByMonth[$count]['month']] = $cCPatientGroupedByMonth[$count]['insertions'];
}`
是什么给我这样的东西:
array:3 [
4 => "1",
8 => "2",
9 => "2"
]
我知道如何使它显示为:
array:3 [
april => "1",
august => "2",
september => "2"
]
抱歉英语不好,谢谢!
您尝试过使用 MONTHNAME() 函数吗?
而不是:
$qb->select(['MONTH(p.createdAt) as month', 'count(ccp) as insertions'])
尝试:
$qb->select(['MONTHNAME(p.createdAt) as month', 'count(ccp) as insertions'])
我有一个查询要获取按月分组的插入次数。我已经安装并运行了 beberlei Doctrine 扩展,但是数组 return 以月份数字作为键。如何在图表行中使用,我想显示月份名称而不是月份编号。
最好的方法是什么?
这个查询:
//get and format the current year
$date = new \DateTime();
$year = $date->format('Y');
//create query
$qb = $this->_em->createQueryBuilder();
$qb->select(['MONTH(p.createdAt) as month', 'count(ccp) as insertions'])
->from(CareConnectPatient::class, 'ccp')
->join('ccp.person', 'p')
->where('YEAR(p.createdAt) = :year')->setParameter('year', $year);
$qb->groupBy('month');
return $qb->getQuery()->getScalarResult();
然后我这样做了,结果是:
`$countMonths = count($cCPatientGroupedByMonth);
for ($count = 0; $count<$countMonths; $count++){
$arrayKeyAsMonth[$cCPatientGroupedByMonth[$count]['month']] = $cCPatientGroupedByMonth[$count]['insertions'];
}`
是什么给我这样的东西:
array:3 [
4 => "1",
8 => "2",
9 => "2"
]
我知道如何使它显示为:
array:3 [
april => "1",
august => "2",
september => "2"
]
抱歉英语不好,谢谢!
您尝试过使用 MONTHNAME() 函数吗?
而不是:
$qb->select(['MONTH(p.createdAt) as month', 'count(ccp) as insertions'])
尝试:
$qb->select(['MONTHNAME(p.createdAt) as month', 'count(ccp) as insertions'])