2015-03-02 77 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