Hello All,
I have just started using Yii2 for a new application. It is still very early days, so I though I’d go with that.
So, starting with the Advanced template, I have extended the User table (on MySQL) to include some additional information about the user. I have also modified the Signup process, so the user ony enters their e-mail, password and the captcha values.
My intention is to force the user enter some of the remaining details at the first instance depending on some business rules. At this point in time, I am trying to force the user enter the rest of the details, AFTER the initial signup.
I created a form for that, that looks like this:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\User */
/* @var $form ActiveForm */
?>
<div class="userSignup">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'a1') ?>
<?= $form->field($model, 'a2') ?>
<?= $form->field($model, 'a3') ?>
<?= $form->field($model, 'b1') ?>
<?= $form->field($model, 'b2') ?>
<?= $form->field($model, 'b3') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div><!-- userSignup -->
actionSignup in SiteController is modified to:
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->render('userSignup', [
'model' => $model,
]);
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
When I go through the signup process, the user gets registered (I can see the record in the database), but code execution fails when rendering the userSignup form with the following error:
Exception (Unknown Property) 'yii\base\UnknownPropertyException' with message 'Getting unknown property: frontend\models\SignupForm::a1'
The value of user.a1 is actually NULL in the table right after the user is saved. However when the user is loaded, even to a new User object (see bellow for modified SignupForm::signup() ), the attribute a1 does not seem to exist in $model (see form code above):
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if($user->save()) {
// using user2 to compare against $user when debugging
$user2 = User::findByEmail($this->email);
return $user2;
}
}
return null;
}
Is this by design?
What would be the best practice to workaround this?
Kind regards,
GeorgiosG