Ejemplo n.º 1
0
 /**
  * назначаем номер "основным" status = 1
  * при этом старый "основной" получает статус текущего номера
  * @param $id
  * @return void|\yii\web\Response
  * @throws NotFoundHttpException
  */
 public function actionDirect($id)
 {
     $model = $this->findModel($id);
     $status = $model->status;
     if ($status == 1) {
         return;
     }
     $dir_model = '';
     try {
         $dir_model = $this->findModel(['employee_id' => $model->employee_id, 'status' => 1]);
     } catch (\Exception $e) {
     }
     $transaction = CellNumbers::getDb()->beginTransaction();
     try {
         $ex = false;
         if ($dir_model) {
             $dir_model->status = null;
             if (!$dir_model->save()) {
                 $ex = true;
             }
         }
         $model->status = 1;
         if (!$model->save()) {
             $ex = true;
         }
         if ($dir_model) {
             $dir_model->status = $status;
             if (!$dir_model->save()) {
                 $ex = true;
             }
         }
         if ($ex) {
             throw new \Exception('Не удалось совершить действие, возможно ошибка валидации...');
         }
         Yii::$app->session->setFlash('success', 'Номер установлен успешно!');
         $transaction->commit();
     } catch (\Exception $e) {
         $transaction->rollBack();
         Yii::$app->session->setFlash('error', $e->getMessage());
     }
     return $this->redirect(['employees/view', 'id' => $model->employee_id]);
 }