Yii - 注册并插入其他 table

Yii - SignUp and insert into other table

所以我想在我的 Yii 应用程序中尝试注册。
但是,我把用户table(用于登录)的关系变成了另一个table。这是关系:

Table用户默认登录注册。但是我想插入另一个数据到user_profile table。我该怎么做?

编辑:
这些是我的代码:

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>

实现目标的一种方法是:

-- 只用一种型号保持控制器清洁。

$model = new SignupForm();

-- 为用户个人资料添加附加字段作为 SignupForm.php 的 属性 并设置必要的规则来验证它们。

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

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

-- 将保存用户配置文件的逻辑放在 signup() 函数中。

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;
}

-- 最后,在视图中添加用户配置文件字段。