PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Yii - SignUp and insert into other table

 February 05, 2022     php, yii, yii2, yii2-advanced-app     No comments   

Issue

So I want to try SignUp into my Yii Application.
But, I was made relationship between user table (for login) into another table. Here is the relation:
enter image description here

Table user is for login and sign up by default. But I want to insert another data to user_profile table. How can I do that?

Edit:
These are my codes:

SiteController.php

public function actionSignup()
{
    $model = new SignupForm();
    $userProfileModel = new UserProfile();

    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome();
            }
        }
    }

    return $this->render('signup', [
        'model' => $model,
        'userProfileModel' => $userProfileModel,
    ]);
}


SignupForm.php

public function signup()
{
    if (!$this->validate()) {
        return null;
    }

    $user = new User();
    //$userProfileModel = new UserProfile();

    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();

    return $user->save() ? $user : null;
}


signup.php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>Please fill out the following fields to signup:</p>

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

                <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

                <?= $form->field($userProfileModel, 'nama')->textInput() ?>

                <?= $form->field($userProfileModel, 'no_hp')->textInput() ?>

                <?= $form->field($model, 'email') ?>

                <?= $form->field($model, 'password')->passwordInput() ?>

                <div class="form-group">
                    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
                </div>

        <?php ActiveForm::end(); ?>
    </div>
</div>


Solution

One way to achieve your goal is :

-- keep the controller clean with only one model.

$model = new SignupForm();

-- add additional fields for user profile as property of SignupForm.php and put necessary rules to validate them.

public $fullname;
public $dateOfBirth;
public $address;
...

public function rules()
{
    ...
    [['fullname', 'dateOfBirth', 'address'], 'required'],
}

-- put the logic to save user profile inside signup() function.

public function signup()
{
    if (!$this->validate()) {
        return null;
    }

    $user = new User();

    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();

    $userProfile = new UserProfile();
    $userProfile->fullname = $this->fullname;
    $userProfile->dateOfBirth = $this->dateOfBirth;
    $userProfile->address = $this->address;

    return $user->save() && ($userProfile->userId = $user->id) !== null && $userProfile->save() ? $user : null;
}

-- last, add the user profile fields in view.



Answered By - Khotim
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing