Answer the question
In order to leave comments, you need to log in
Yii2. Why are field data not being passed to the database?
There is a ready CMS for yii2. I want to add a new friend
field on the registration page.
But my field is not being written to the db.
Entered it in scripts in the User model
public $friend;
public function scenarios()
{
return [
'signup' => ['username', 'email', 'password', 'first_name', 'last_name', 'friend'],
public $friend;
public function signup()
{
if ($this->validate() === true) {
$user = new User;
$user->setScenario('signup');
$user->username = $this->username;
$user->password = $this->password;
$user->friend = $this->friend;
$user->email = $this->email;
if ($user->save() === false) {
$this->addErrors($user->errors);
return null;
}
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<?= $form->field($model, 'first_name') ?>
<?= $form->field($model, 'last_name') ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'friend') ?>
<?= $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(); ?>
Answer the question
In order to leave comments, you need to log in
Try to remove the public $friend model property declarations;
You also need to check the data type for the table field and the type of sent data for the public field
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question