view->fetch(); } /** * 根据生日计算年龄、类型 * @param Student $student * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function setInfoByBirthday(Student $student ){ $return = ['code'=>0,'msg'=>'无改变']; //年龄改变的学生 $list = Student::field('id,age,TIMESTAMPDIFF(YEAR,`birthday`,CURDATE()) as age_now') ->where('status','normal') ->having('age <> age_now') ->select(); if($list){ $data = []; foreach ($list as $v){ $data[] = [ 'id' => $v['id'], 'age' => $v['age_now'], 'type' => $v['age_now']<11 ? 1 : 2, ]; } $res = $student->isUpdate()->saveAll($data); if($res){ $return = ['code'=>1,'msg'=>count($data).'位学生改变:'.implode(',',array_column($data,'id'))]; }else{ $return['msg'] = '改变失败'; } } echo json_encode($return, true); } }