/**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @return string|\yii\web\Response
  */
 public function actionCreate()
 {
     $user = new User(['scenario' => 'create']);
     $role = new Role();
     if ($user->load(Yii::$app->request->post()) && $role->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $role])) {
         $user->setPassword($user->password);
         $user->generateAuthKey();
         if ($user->save()) {
             $role->user_id = $user->getId();
             $role->save();
         }
         return $this->redirect('index');
     } else {
         return $this->render('create', ['user' => $user, 'role' => $role]);
     }
 }
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @return string|\yii\web\Response
  */
 public function actionCreate()
 {
     $user = new User(['scenario' => 'create']);
     $role = new Role();
     if ($user->load(Yii::$app->request->post()) && $role->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $role])) {
         $user->setPassword($user->password);
         $user->generateAuthKey();
         if ($user->save()) {
             $role->user_id = $user->getId();
             $role->save();
         }
         Yii::$app->session->setFlash('success', implode(' ', [ucfirst($user->username), ' successfully created!']));
         return $this->redirect('index');
     } else {
         return $this->render('create', ['user' => $user, 'role' => $role]);
     }
 }
示例#3
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @return string|\yii\web\Response
  */
 public function actionCreate()
 {
     $user = new User(['scenario' => 'create']);
     $role = new Role();
     if ($user->load(Yii::$app->request->post()) && $role->load(Yii::$app->request->post()) && User::validateMultiple([$user, $role])) {
         /* Gets the image instance and uploads image to the specified directory
          * and then saves the model data */
         $image = UploadedFile::getInstance($user, 'image');
         $image->saveAs('uploads/' . $image->baseName . '.' . $image->extension);
         $user->image = $image->baseName . '.' . $image->extension;
         $user->setPassword($user->password);
         $user->generateAuthKey();
         if ($user->save()) {
             $role->user_id = $user->getId();
             $role->save();
         }
         return $this->redirect('index');
     } else {
         return $this->render('create', ['user' => $user, 'role' => $role]);
     }
 }