ActiveRecord插入空字段Yii2

时间:2015-03-02 23:16:43

标签: php mysql yii2

在添加新记录时,只插入ID,其他字段为空。

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 显示所有必要的数据都带入 $ 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;
    }
}

截图表用户:

[包含emptys字段的表] http://i.stack.imgur.com/EowUl.jpg
  [表格结构] http://i.stack.imgur.com/lpdZM.jpg

我什么都没改变,只是我添加了新用户注册的动作

1 个答案:

答案 0 :(得分:13)

您已将数据库字段声明为公共类字段($ id,$ username等),因此将分配此字段而不是内部ActiveRecord字段。尝试删除或注释掉课程公共字段。