W
W
webanalytics2016-02-05 21:56:37
Yii
webanalytics, 2016-02-05 21:56:37

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'],

in the registration form model:
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;
            }

view :
<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(); ?>

in the debugger, it says that all the entered user data was transferred by the POST method successfully. But only the friend field is empty in the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Bukharev, 2016-02-05
@webanalytics

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 question

Ask a Question

731 491 924 answers to any question