frontend\models\ChangeMobileForm::change PHP Method

change() public method

public change ( $runValidation = true )
    public function change($runValidation = true)
    {
        if ($runValidation && !$this->validate()) {
            return false;
        }
        $user = Yii::$app->user->identity;
        $user->mobile = $this->mobile;
        return $user->save(false);
    }

Usage Example

Example #1
0
 /**
  * 更换手机号
  * 
  * @return array
  */
 public function actionMobile($step = '1')
 {
     if ($step === '1') {
         $model = new ChangeMobileSendRequestForm();
         if ($model->load(Yii::$app->request->post(), '') && $model->sendMsg()) {
             return ['status' => 'success', 'data' => []];
         } else {
             return ['status' => 'fail', 'data' => ['errors' => $model->getErrors('mobile') ?: [Yii::$app->smser->message]]];
         }
     } elseif ($step === '2') {
         $model = new ChangeMobileForm();
         $model->mobile = Yii::$app->session['mobileChange'];
         // TODO
         if ($model->load(Yii::$app->request->post(), '') && $model->change()) {
             return ['status' => 'success', 'data' => ['mobile' => Yii::$app->user->identity->mobile]];
         } else {
             return ['status' => 'fail', 'data' => ['errors' => $model->getErrors()]];
         }
     } else {
         throw new BadRequestHttpException('参数错误!');
     }
 }