/** * Сохраняем участников или консультантов * * @param $data array * @param $nType integer */ public function savePersons($data, $nType) { $bOk = true; Person::updateAll(['prs_active' => 0, 'prs_type' => 0, 'prs_sec_id' => 0, 'prs_doc_id' => 0], ['prs_doc_id' => $this->doc_id, 'prs_type' => $nType]); foreach ($data as $ob) { $ob['prs_type'] = $nType; $ob['prs_doc_id'] = $this->doc_id; $ob['prs_active'] = Person::PERSON_STATE_ACTIVE; $ob['prs_sec_id'] = $this->doc_sec_id; // Yii::info('consultant = ' . print_r($ob, true)); $s = 'Update ' . Person::tableName() . ' Set '; $param = []; $sDelim = ''; if (empty($ob['prs_org']) && !empty($ob['prs_hischool'])) { $ob['prs_org'] = $ob['prs_hischool']; } foreach ($ob as $k => $v) { $s .= $sDelim . $k . ' = ' . ':' . $k; $param[':' . $k] = $v; $sDelim = ', '; } $s .= ' Where prs_active = 0 And prs_type = 0 And prs_sec_id = 0 And prs_doc_id = 0 Limit 1'; // Yii::info($s); $n = Yii::$app->db->createCommand($s, $param)->execute(); if ($n == 0) { $oNew = new Person(); $oNew->prs_type = $nType; $aTmp = $oNew->scenarios(); $oNew->attributes = $ob; $oNew->prs_doc_id = $this->doc_id; $oNew->prs_active = Person::PERSON_STATE_ACTIVE; $oNew->prs_sec_id = $this->doc_sec_id; if (!$oNew->save()) { $bOk = false; Yii::info('savePersons() nType = ' . $nType . ' attributes: ' . print_r($oNew->attributes, true)); Yii::info('savePersons() nType = ' . $nType . ' Error save oNew: ' . print_r($oNew->getErrors(), true)); } } } return $bOk; }
/** * Creates a new Person model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Person(); $jobModel = new Job(); if ($jobModel->load(Yii::$app->request->post()) && $jobModel->validate() && $model->load(Yii::$app->request->post()) && $model->validate()) { $transaction = $model->getDb()->beginTransaction(); try { if (is_numeric($jobModel->name)) { $jobModel = $jobModel->findOne($jobModel->name); } else { $jobModel = new Job(); $jobModel->load(Yii::$app->request->post()); if (!$jobModel->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($jobModel->tableName())), 'msj' => print_r($jobModel->getErrors(), true)]), 500); } } $model->job_id = $jobModel->id; if (!$model->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->getErrors(), true)]), 500); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'jobModel' => $jobModel]); } }