Add field to user table

Hi I’m trying to add another field to the default user table but I can’t save this new field, it keeps saving a NULL value

If echo a var_dump in SignupForm.php with $this->test it shows what the user send trough the form

signup:




 <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

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

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

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

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

                <div class="form-group">

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

                </div>

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



signupForm:




public function signup()

    {

        if ($this->validate()) {

            $user = new User();

            $user->username = $this->username;

            $user->email = $this->email;

            $user->setPassword($this->password);

            $user->generateAuthKey();

            if ($user->save()) {

                return $user;

            }

        }


        return null;

    }



sitecontroller:




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->goHome();

                }

            }

        }


        return $this->render('signup', [

            'model' => $model,

        ]);

    }



User.




class User extends ActiveRecord implements IdentityInterface

{


    const STATUS_DELETED = 0;

    const STATUS_ACTIVE = 10;

    public $test;


    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return '{{%user}}';

    }


    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            TimestampBehavior::className(),

        ];

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            ['status', 'default', 'value' => self::STATUS_ACTIVE],

            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],

            ['test' , 'string', 'max' => 45],

        ];

    }



I just can’t find the right way to alter the default User table in order to add fields :(

Thanks in advance.

Here you are missing ‘test’ field




public function signup()

    {

        if ($this->validate()) {

            $user = new User();

            $user->username = $this->username;

            $user->email = $this->email;

            $user->setPassword($this->password);


            // You have to add test assignment

            $user->test = $this->test;


            $user->generateAuthKey();

            if ($user->save()) {

                return $user;

            }

        }


        return null;

    }



yes I’ve been trying that…

if I echo a var_dump($this->test)




public function signup()

    {


echo var_dump($this->test);


        if ($this->validate()) {

            $user = new User();

            $user->username = $this->username;

            $user->email = $this->email;

            $user->setPassword($this->password);


            // You have to add test assignment

            $user->test = $this->test;


            $user->generateAuthKey();

            if ($user->save()) {

                return $user;

            }

        }


        return null;

    }



it shows whatever the user wrote in the input… but

this line $user->test = $this->test; doesn’t save the value in the database :(

Right!

You must remove $test field at top of model declaration,

otherwise only this property will be updated, but not db field.




class User extends ActiveRecord implements IdentityInterface

{


    const STATUS_DELETED = 0;

    const STATUS_ACTIVE = 10;


    // --------------------

    // --- You have to remove this!!!

    // ---------------------

    public $test;


    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return '{{%user}}';

    }




That worked, thanks a lot! :D