public function actionCreate($patientId) { $patient = Patient::model()->findByPk($patientId); $model = new SurgeryRecord(); $doctors = Doctor::model()->findAll(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['SurgeryRecord'])) { $model->attributes = $_POST['SurgeryRecord']; if ($model->save()) { Yii::app()->user->setFlash('success', '儲存成功.'); $this->redirect($this->createUrl('/patient/update/', array('id' => $patient->id))); } } $this->render('create', array('patient' => $patient, 'model' => $model, 'doctors' => $doctors)); }
/** * 匯入手術記錄 */ public function actionSurgeryRecords() { $this->currentActive = '手術記錄'; $excel = new ExcelUploadForm(); $rows = array(); $errors = array(); if (isset($_POST['ExcelUploadForm'])) { $excel->attributes = $_POST['ExcelUploadForm']; $excel->file = CUploadedFile::getInstance($excel, 'file'); $transaction = Yii::app()->db->beginTransaction(); try { if ($excel->validate()) { $rows = $excel->getContents(); foreach ($rows as $i => $row) { if ($i != ExcelUploadForm::HEADER) { $surgeryRecord = new SurgeryRecord(); $surgeryRecord->patient_id = $row[1]; $surgeryRecord->date = $row[3]; $surgeryRecord->pay_type = $this->_getCovertPayType($row[4]); // 1=健保, 2=自費 $surgeryRecord->icd = $row[5]; $surgeryRecord->surgical_methods = $row[6]; $surgeryRecord->three_dimensional = $row[7]; $surgeryRecord->dentist_id = $row[8]; $surgeryRecord->plastic_surgeon_id = $row[10]; $surgeryRecord->memo = $row[12]; $surgeryRecord->is_surgical_priority = $this->_getConvertChecked($row[13]); $surgeryRecord->is_outside_dentist = $this->_getConvertChecked($row[14]); if (!$surgeryRecord->save()) { $errorData = array('第' . ($i + 1) . '筆,' . $row[2] => $surgeryRecord->getErrors()); $errors[] = $errorData; } } } $transaction->commit(); } else { $errors[] = array('Excel' => $excel->getErrors()); } } catch (Exception $e) { $transaction->rollBack(); $errors = $e->getMessage(); } $this->_setFlashMessage($errors); $this->redirect($this->createUrl("/excel/import/{$this->action->id}")); } $this->render('upload', array('excel' => $excel)); }