默认情况下
all()
返回模型数组。如果要像这样构建自定义查询,请在
select()
,您需要使用
asArray()
强制将行作为数组返回,而不是
ActiveRecord
对象(无法从查询结果中创建)。
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$data = MachineLog::find()
->select(['sum(value)', 'max(datetime)'])
->where([ '<', 'datetime', $end->format('Y-m-d G:i:s') ])
->andWhere([ '>', 'datetime', $start->format('Y-m-d G:i:s') ])
->andWhere([ 'type' => $type, 'machine_id' => $machineId[0] ])
->groupBy(["UNIX_TIMESTAMP(datetime) DIV $diffInSeconds"])
->asArray()
->all();
return $data;