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。これを使用するにはどうすれば変更できますか?

4

0 に答える 0