看了上面的效果有没有想过是怎么做到的?
public function search()
{
if (!$this->validate()) {
return $this->errorResponse;
}
list($begin_date, $end_date) = self::getDate($this->month);
$query = MiaoshaGoods::find()->alias('mg')
->select('mg.open_date,COUNT(mg.id) as miaosha_count')
->where([
'AND',
['mg.is_delete' => 0, 'mg.store_id' => $this->store_id,],
['>=', 'mg.open_date', $begin_date],
['<=', 'mg.open_date', $end_date],
])->groupBy('mg.open_date')->orderBy('mg.open_date ASC');
$list = $query->asArray()->all();
$new_list = [];
foreach ($list as $item) {
$new_list[str_replace('-', '', $item['open_date'])] = $item; //值得学习的地方!!!
}
//file_put_contents("log.txt", print_r($new_list,1).PHP_EOL, FILE_APPEND);
return [
'code' => 0,
'data' => [
'list' => $new_list,
],
];
}
//这个方法就可实现每月的开始日期和结束日期不怕搞错,很好!!!
public static function getDate($month)
{
$month = intval($month);
$begin_date = date('Y-m-d', strtotime(date('Y-m-01') . " {$month} month"));
$end_date = date('Y-m-d', strtotime(date('Y-m-01') . " " . ($month + 1) . " month -1 day"));
return [$begin_date, $end_date];
}
那个值得学习的地方!!!你猜他的效果是怎样的?其实值不会覆盖!