Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord Inserting empty fields Yii2

Tags:

php

mysql

yii2

On adding a new record, insert only ID, other fields empty is added.

public function actionSignup()
{
    $model = new User();

    if(Yii::$app->request->post()) {
        $model->id = 122;
        $model->username = 'user123';
        $model->password = 'pass123';
        ...
        $model->save(false);
    }
    return $this->render('signup',['model' => $model]);
}

var_dump shows that all necessary data are brought in $model...

User model:

<?php

namespace app\models;

use yii\captcha\Captcha;
use yii\db\ActiveRecord;

class User extends ActiveRecord implements \yii\web\IdentityInterface
{
    public $id;
    public $username;
    public $password;
    public $user_fio;
    public $user_email;
    public $user_group;
    public $verifyCode;

    private static $users;

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'index' : null,
            ],
        ];
    }

    public function rules()
    {
        return [
            [['username', 'user_fio', 'user_email', 'password', 'user_group'], 'required'],
            [['username', 'user_fio', 'password'], 'string', 'max' => 70],
            [['user_email'], 'string', 'max' => 150],
            ["verifyCode", "captcha", 'captchaAction' => 'site/captcha'],
        ];
    }

    public static function tableName()
    {
        return '{{users}}';
    }

    public function attributesLabels()
    {
        return [
            'username' => 'username',
            'password' => 'password',
            'user_fio' => 'fiouser',
            'user_mail' => 'email',
            'user_group' => 'group',
            'verifyCode' => 'code with image'
        ];
    }

    public static function findIdentity($id)
    {
        $user = Users::find()->where(['id' => $id])->one();

        if(!count($user)) return null;
        else
            return new static($user);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        $user = Users::find()->where(['auth_key' => $token])->one();

        if(!count($user)) return null;
        else
            return new static($user);
    }

    public static function findByUsername($username)
    {
        $user = Users::find()->where(['username' => $username])->one();

        if(!count($user)) return null;
        else
            return new static ($user);
    }

    public function getId()
    {
        return $this->id;
    }


    public function getAuthKey()
    {
        return $this->authKey;
    }


    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    public function validatePassword($password)
    {
        return $this->password === $password;
    }
}

screenshots table Users:

[Table with emptys fields] http://i.stack.imgur.com/EowUl.jpg
[Table structure] http://i.stack.imgur.com/lpdZM.jpg

I changed nothing, only I added action of registration of the new user

like image 595
Yur4k Avatar asked Dec 05 '22 22:12

Yur4k


1 Answers

You have declared database fields as public class fields ($id, $username, etc.), so this fields are assigned instead of internal ActiveRecord fields. Try to delete or comment out class public fields.

like image 177
Edin Omeragic Avatar answered Dec 15 '22 10:12

Edin Omeragic