[英]Attempting to return users registered per month, but not getting a correct count in CakePHP 3?
I am attempting to fetch a count of users who have registered each month, separated by month in the results - so, January - 22, February - 36, March- 56, so on and so forth limited to this month and the last five. 我试图获取每月注册的用户数,结果中按月分隔-因此,1月-22日,2月-36日,3月-56日,依此类推,以此类推,仅限于本月和最后五个月。
However, I can't seem to get the query correct as it relates to CakePHP 3's query builder. 但是,我似乎无法使查询正确,因为它与CakePHP 3的查询生成器有关。 This is what I have so far: 这是我到目前为止的内容:
$query = $this->find('all');
$query->select(['count' => $query->func()->count('id'), 'day' => 'DAY(dateRegistered)']);
$query->where(['YEAR(dateRegistered)' => date('Y')]);
$query->group(['MONTH(dateRegistered)']);
$query->enableHydration(false);
return $query->all();
This seems to return the user count for the month, but that's all, and not in an easily graph-able format. 这似乎返回了该月的用户数,但仅此而已,而并非以易于图形化的格式。 I have seen some queries in raw SQL so it should be possible, but I would like to use the native query controls in Cake. 我已经在原始SQL中看到了一些查询,因此应该可以,但是我想在Cake中使用本机查询控件。 Could someone help fix this? 有人可以帮忙解决此问题吗?
Try this: 尝试这个:
$query = $this->find();
$res = $query
->select([
'month'=>'monthname(created)',
'count'=>$query->func()->count('*'),
])
->where('year(created) = year(curdate())')
->group('month')
->enableHydration(false);
pr($res->toArray());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.