0%

cc-develop-note-iii

导入新生,user存储成功,student、assignment存储失败

1
2
3
4
if (!$stu->save()) {
$res['errors'] = $stu->errors;
throw new \Exception(implode($stu->errors));
}
1
errors: {SID: ["Sid must be a string."]}
1
{"success":false,"errors":{"TutorID":["Tutor ID must be an integer."]},"data":"student 保存失败!"}

==注意属性类型!==

PHP 转换函数: intval()、floatval()、strval()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//批量导入新生,记录日志,成功时返回新生SID集合
public static function batchFreshmanStudentUpload($data){
date_default_timezone_set('PRC');
$log = LogBatchModel::beforeBatch($data);
$res['success'] = false;
if($log['success']===false)
{
//创建批量导入的日志时出现错误
$res['logBatch']=$log;
return $res;
}
$LBID=$log['data']['LBID'];
$filePath=$data['FilePath'];
try{
//获得新生集合
$StudentArray = self::readFreshmanFromExcel($filePath);
}catch (Exception $e)
{
//解析 Excel 出现错误
$res['filePath']=$filePath;
return $res;
}
// 存入数据库
$key=['SID','StudentName','Grade','Education','Major','IDCard','Passwd','Tutor'];// 与模型的属性名相对应

$transaction=Yii::$app->db->beginTransaction('SERIALIZABLE');
try {
for ($i = 0; $i < count($StudentArray); $i++)
{
$row = $StudentArray[$i][0];
$student=[];
for($j=0;$j<count($key);$j++)
{
$student[$key[$j]]=$row[$j];
}
if($student['SID']===null)throw new \Exception('学号不能为空!');
// Sid must be a string.
$student['SID']=strval($student['SID']);
if($student['StudentName']===null)throw new \Exception('姓名不能为空!');
// 查询 EducationLevelID
$EducationLevel=EducationLevelModel::findOne(['LevelName' => $student['Education']]);
if($EducationLevel===null)throw new \Exception('层次不能为空!');
$EducationLevelID = $EducationLevel['ELID'];
// 查询 MID
$Major=MajorModel::findOne(['EducationLevelID'=>$EducationLevelID,'MajorName'=>$student['Major']]);
if($Major===null)throw new \Exception('专业不能为空!');
$MID = $Major['MID'];
// 创建 MajorEnrollyear 然后查询 MEYID
$majorEnrollYear=new MajorEnrollyearModel();
$majorEnrollYear->Grade=$student['Grade'];
if($majorEnrollYear===null)throw new \Exception('年级不能为空!');
$majorEnrollYear->MajorID=$MID;
$majorEnrollYear->save();
$MajorEnrollyear=MajorEnrollyearModel::findOne(['MajorID'=>$MID,'Grade'=>$student['Grade']]);
if($MajorEnrollyear===null)throw new \Exception('学号 '.$student['SID'].' 的年级、专业、层次对应关系错误!');
$student['MajorEnrollYearID']=$MajorEnrollyear['MEYID'];

// 查询 TutorID
$Tutor=TeachersModel::findOne(['TeacherName'=>$student['Tutor']]);
if($Tutor===null)
$student['TutorID']=null;
else
$student['TutorID']=intval($Tutor['TID']);

// 创建 user
if($student['Passwd']===null)$student['Passwd']='111111';
$student['Passwd']=hash_hmac('SHA256', 'cc-frontend-vue',$student['Passwd']);
$user = new User();
$user->username = $student['StudentName'];
$user->setPassword($student['Passwd']);
$user->generateAuthKey();
if (!$user->save()){
$res['errors'] = $user->errors;
throw new \Exception("user 保存失败!");
}

// 创建学生
$stu = new StudentModel();
$stu->id = $user->id;
$stu->MajorEnrollYearID =$student['MajorEnrollYearID'];
$stu->SID = $student['SID'];
$stu->StudentName = $student['StudentName'];
$stu->TutorID=$student['TutorID'];
$stu->IDCard=$student['IDCard'];
if (!$stu->save()) {
$res['errors'] = $stu->errors;
throw new \Exception("student 保存失败!");
}

// 创建 auth_assignment
$assignment = new Assignment($user->id);
$res['authRes']=$assignment->assign(['学生','游客']);
if($res['authRes']!==2)
{
throw new \Exception("auth 保存失败!");
}
}
$transaction->commit();
for ($i = 0; $i < count($StudentArray); $i++)
$res['data'][] = $StudentArray[$i][0][0];
}catch (\Exception $exception) {
$transaction->rollBack();
LogBatchModel::afterBatch(false,$LBID);
$res['data'] = $exception->getMessage();
return $res;
}
LogBatchModel::afterBatch(true,$LBID);
$res['success'] = true;
return $res;
}

新导入学生可以成功登陆。

表格上传失败时进行重置。