Esempio n. 1
0
 /**
  * Updates an existing Group model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $user = new User();
     $advisor = new Advisor();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $advisorArray = Yii::$app->request->post('id_advisor');
         if (empty($advisorArray)) {
             $advisorArray = [];
         }
         $advisorGroupArray = ArrayHelper::map(AdvisorGroup::findAll(['id_group' => $id]), 'id_advisor', 'id_advisor');
         $delArray = array_diff($advisorGroupArray, $advisorArray);
         foreach ($delArray as $delAdvisorId) {
             $delAdvisorGroup = AdvisorGroup::findOne(['id_group' => $model->primaryKey, 'id_advisor' => $delAdvisorId]);
             $delAdvisorGroup->delete = 1;
             $delAdvisorGroup->save();
         }
         $addAdvisorGroupArray = array_diff($advisorArray, $advisorGroupArray);
         $advisorGroup = new AdvisorGroup();
         $advisorGroup->id_group = $id;
         foreach ($addAdvisorGroupArray as $key => $value) {
             $group = clone $advisorGroup;
             $group->id_advisor = $value;
             $group->save();
         }
         return $this->redirect(['index']);
     } else {
         return $this->render('update', ['model' => $model, 'user' => $user, 'advisor' => $advisor]);
     }
 }
Esempio n. 2
0
 public function UserUpdate($role, $content, $id)
 {
     switch ($role) {
         case Dict::USER_ROLE_BETTERDEBT:
             $model = new Betterdebt();
             $modelkey = "Betterdebt";
             break;
         case Dict::USER_ROLE_ADVISOR:
             $model = new Advisor();
             $modelkey = "Advisor";
             break;
         case Dict::USER_ROLE_CLIENT:
             $model = new Client();
             $modelkey = "Client";
             break;
         case Dict::USER_ROLE_BANK:
             $model = new BankUser();
             $modelkey = "BankUser";
             break;
         default:
             break;
     }
     if (empty($content['id_group'])) {
         $content['id_group'] = [];
     }
     $model = $model::findOne($id);
     $user = static::find()->where(['id_user' => $model->id_user])->one();
     $userOld = static::find()->where(['id_user' => $model->id_user])->one();
     //事务开始
     $transaction = Yii::$app->db->beginTransaction();
     //保存到用户信息主表
     if (isset($content['User']['email'])) {
         $email = $content['User']['email'];
     } elseif (isset($content['User']['new_email'])) {
         $email = $content['User']['new_email'];
     }
     foreach ($content['User'] as $key => $value) {
         $user->{$key} = $value;
     }
     if ($userOld['email'] != $email) {
         $user->email = $userOld['email'];
         $user->new_email = $email;
         // 发送修改邮件的email
         if (!$this->changEmail($user)) {
             $this->_istrue = false;
         }
     }
     if (!$user->save()) {
         $this->_istrue = false;
     }
     //保存到用户信息表(advisor/client/betterdebt/bank_user)
     foreach ($content[$modelkey] as $key => $value) {
         $model->{$key} = $value;
         if ($key == 'date_of_birth' || $key == 'identification_expiration_date') {
             $model->{$key} = strtotime($value);
         }
     }
     if (!$model->save()) {
         $this->_istrue = false;
     }
     if ($modelkey == "Betterdebt") {
         if (empty($model->is_primary_service_member)) {
             //判断是否设置为primary loan officer
             $model->is_primary_service_member = Dict::BD_PRIMARY_MEMBER_NOT;
         } else {
             /**
              * 如果选择了设置为primary loan officer,则将其他的自动调回不是primary
              */
             $rows = Betterdebt::updateAll(['is_primary_service_member' => Dict::BD_PRIMARY_MEMBER_NOT], "id_betterdebt not in ({$model->id_betterdebt}) ");
         }
     }
     //修改advisor时特有的
     $advisorGroups = AdvisorGroup::findAll(['id_advisor' => $id]);
     $groupArray = ArrayHelper::map($advisorGroups, 'id_group', 'id_group');
     $delgroupArray = array_diff($groupArray, $content['id_group']);
     foreach ($delgroupArray as $delGroupId) {
         $advisorGroup = AdvisorGroup::findOne(['id_group' => $delGroupId, 'id_advisor' => $model->primaryKey]);
         $advisorGroup->delete = 1;
         if (!$advisorGroup->save()) {
             //$this->_istrue = false;
         }
     }
     /*
             $advisorgroup_del = new AdvisorGroup();
             if ($advisorgroup_del->findAll(['id_advisor' => $id])){
        if (!$advisorgroup_del->deleteAll(['id_advisor' => $id])){
            $this->_istrue = false;
        }
             }
     */
     if (!empty($content['id_group'])) {
         $addAdvisorGroupArray = array_diff($content['id_group'], $groupArray);
         $advisorgroup = new AdvisorGroup();
         $advisorgroup->id_advisor = $model->primaryKey;
         foreach ($addAdvisorGroupArray as $key => $value) {
             $insertadvisorgroup = clone $advisorgroup;
             $insertadvisorgroup->id_group = $value;
             if (!$insertadvisorgroup->save()) {
                 $this->_istrue = false;
             }
         }
     }
     //事务结束提交
     if ($this->_istrue) {
         $transaction->commit();
         return true;
     } else {
         $transaction->rollback();
         return false;
     }
 }