PHP UPSERT

// 学校日誌設定
		$school_diary_setting = $this->SchoolDiarySetting_m->getSettingData($this->login_data['school_id'], $this->login_data['year']);
		// 学校日誌電子印設定
		$school_diary_stamp = array();
		if(!empty($school_diary_setting['id'])) {
			$school_diary_stamp = array_column($this->SchoolDiaryStamp_m->getDataByDiarySettingId($school_diary_setting['id']), null ,'id');
		}

		if(!empty($this->input->post())) {
			$post_data = $this->input->post();
			// トランザクション開始
			$this->db->trans_begin();
			if(empty($post_data['school_diary_setting_id'])) {
				$school_diary_setting_id = $this->SchoolDiarySetting_m->insertData($this->login_data['school_id'], $this->login_data['teacher_id'], $this->login_data['year'], $post_data);
				if(!empty($post_data['school_diary_stamp'])) {
					foreach($post_data['school_diary_stamp'] as $data) {
						$this->SchoolDiaryStamp_m->insertData($school_diary_setting_id, $this->login_data['teacher_id'], $data);
					}
				}
			} else {
				$this->SchoolDiarySetting_m->updateData($this->login_data['school_id'], $this->login_data['teacher_id'], $this->login_data['year'] ,$post_data);
				$school_diary_stamp_ids = array_column($school_diary_stamp, 'id');
				$post_school_diary_stamp_ids = array_column($post_data['school_diary_stamp'], 'school_diary_stamp_id');
				$post_school_diary_stamps = array_column($post_data['school_diary_stamp'], null, 'school_diary_stamp_id');
				$post_school_diary_stamps_insert = array_filter($post_data['school_diary_stamp'], function($v) {
					return $v['school_diary_stamp_id'] == '';
				});
				if($post_data['use_stamp'] == 1) {
					foreach($school_diary_stamp_ids as $id) {
						if(in_array($id, $post_school_diary_stamp_ids, true)) {
							$this->SchoolDiaryStamp_m->updateData($id, $post_school_diary_stamps[$id], $this->login_data['teacher_id']);
						} else {
							$this->SchoolDiaryStamp_m->deleteData(array( '0' => $id), $this->login_data['teacher_id']);
						}
					}
					foreach($post_school_diary_stamps_insert as $insert_data) {
						$this->SchoolDiaryStamp_m->insertData($post_data['school_diary_setting_id'], $this->login_data['teacher_id'], $insert_data);
					}
				} else {
					$this->SchoolDiaryStamp_m->deleteData($school_diary_stamp_ids, $this->login_data['teacher_id']);
				}
			}
			// 標準レイアウトに切り替えた時(初めて切り替えた場合のみ走るから「使用しない」から「使用する」への切り替えのみ。)
			if(empty($school_diary_setting) && empty($post_data['school_diary_setting_id']) && $post_data['use_school_diary'] = 1) {
				$new_school_diary_fields = $this->getStandardLayoutField();
				foreach($new_school_diary_fields as $new_school_diary_field) {
					$this->SchoolDiaryField_m->insert($new_school_diary_field);
				}
			}
			if($this->db->trans_status() !== false) {
				$this->db->trans_complete();
				$this->db->trans_commit();
				echo json_encode(array('success' => '成功'));
				return;
			} else {
				$this->db->trans_rollback();
				echo json_encode(array('error' => 'データベースの登録に失敗しました。'));
				return;
			}
		}

コメント

タイトルとURLをコピーしました