YII2活动记录查询每周获取记录

时间:2016-08-26 06:06:06

标签: yii2-advanced-app

根据date_received列,我试图将每周的记录假设为星期一作为一周的第一天。

此原始查询的等效活动记录查询应该是什么?

$week = $connection->createCommand('SELECT * FROM dts_document WHERE date_received > DATE_SUB(NOW(), INTERVAL 1 WEEK)');

我尝试了以下操作,但它返回了整个记录。

$week = DtsDocument::find()->where(['>','date_received', 'DATE_SUB(NOW(), INTERVAL 1 MONTH)'])->count();

1 个答案:

答案 0 :(得分:2)

yii\db\Expression包裹DATE_SUB部分以防止引用:

use yii\db\Expression;

...

$week = DtsDocument::find()
    ->where(['>', 'date_received', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')])
    ->count();