我目前有这个:
$firstDay = new DateTime('first day of this month 00:00:00');
$lastDay = new DateTime('first day of this month 00:00:00');
return $this->Visitor->query(
"SELECT
(? + INTERVAL c.day - 1 DAY) unix_ts_day,
COUNT(v.site_id) as visit_count
FROM
calendar c
LEFT JOIN (
SELECT * FROM visitors
WHERE site_id = ? AND DATE(created) BETWEEN ? AND ?
) v
ON DAYOFMONTH(v.created) = c.day
GROUP BY
unix_ts_day", array(
$firstDay->format('Y-m-01' ),
$this->id,
$firstDay->format('Y-m-01' ),
$lastDay->format('Y-m-t' )
)
);
我的表:
Calendar
id | day
---------
1 | 1
2 | 2
3 | 3
...
31 | 31
Visitors
id | site_id | created
-----------------------------------
1 | 16 | 2012-10-18 11:14:39
2 | 16 | 2012-10-18 11:15:17
3 | 11 | 2012-10-18 11:49:14
4 | 11 | 2012-10-18 11:49:43
5 | 16 | 2012-10-19 11:54:37
6 | 1 | 2012-10-19 05:56:31
7 | 2 | 2012-10-19 05:57:56
现在,Calendar 表(没有 Calendar 模型)与 Visitor 表没有关联。
虽然上述查询正在工作。我在想这是否是 CakePHP 类可以做的事情,而不是我调用该query
方法。如何更改它以使用它?