我创建新用户时没有数据保存

时间:2014-05-05 13:39:36

标签: php yii2

我正在尝试创建新用户

我修改了这段代码

 public function actionCreate() {
   $model = new Users;

   if(isset($_POST['Users'])) {
      if($model->load(Yii::$app->request->post())) {
         if($model->save()){
            return $this->redirect(['']);
         }
      }

   } else {
      return $this->render('create', [
           'model' => $model,
      ]);
   }

}

但它没有保存!! 怎么能找到问题??

2 个答案:

答案 0 :(得分:0)

试试这个 -

$model = new Users; if(isset($_POST['Users'])) { $model->attributes = $_POST['Users']; //instead of if($model->load(... if($model->save()){ return $this->redirect(['']); } ..... ....

$model->attributes = $_POST['Users'];将获取发布的值并完美设置$model对象,除非您为$model->load( [.....] )调用了一些额外的代码。

正如@Alex所要求的那样,你需要提到$model->load()究竟是什么。

我希望它有所帮助。

答案 1 :(得分:0)

试试这个

   public function actionCreate()
  {
    $model = new users;

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['']);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
  }

希望它有效!