2015-03-02 75 views
3

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

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表明,所有必要的数据都在$模型 ...带来

用户模式:

<?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; 
    } 
} 

截图表用户:

[表空洞字段] http://i.stack.imgur.com/EowUl.jpg
[表结构] http://i.stack.imgur.com/lpdZM.jpg

我什么都没有改变,只是我添加新用户注册

+0

你有调试吗?你可以看到插入查询。 – ankitr 2015-03-03 05:13:02

+0

在调试中:INSERT INTO'users'('id')VALUES(NULL) – Yur4k 2015-03-03 06:43:21

回答

12

您已经声明数据库字段作为公共类字段($ ID,$用户名等),所以这个行动字段被分配而不是内部ActiveRecord字段。尝试删除或注释掉公开字段。

+2

谢谢当删除所有公共字段时,它工作) – Yur4k 2015-03-03 06:48:04