2016-08-26 135 views
2

我試圖按照date_received列的週數假設星期一爲第一天。YII2活動記錄查詢獲取記錄每週

什麼應該是這個原始查詢的等效活動記錄查詢?

$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(); 

回答

2

DATE_SUB部分與yii\db\Expression防止報價:

use yii\db\Expression; 

... 

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