使用CakePHP重建查询

时间:2012-10-24 04:00:15

标签: cakephp cakephp-2.0

我目前有这个:

    $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方法。如何更改以使用它?

0 个答案:

没有答案