插入数据是空白,求解? [ 2.0 版本 ]
/**
* 注册
*
* @return bool
*/
public function userReg()
{
$Cls = new User();
$Cls->user_id = time() . '_' . rand(0, 999);
$Cls->username = $this->username;
$Cls->password = $Cls->setPassword($this->password);
$Cls->nickname = $this->nickname;
$Cls->telphone = $this->telphone;
$Cls->grade = 4;
// 生成 "remember me" 认证key
$Cls->generateAuthKey();
if (!$Cls->save(false)) {
return false;
}
return $Cls;
}
LoginForm,表单里面的函数
/**
* 注册
*/
public function actionReg()
{
$model = new LoginForm();
$model->scenario = 'reg';
if (Yii::$app->request->isAjax) {
if (!$model->load(Yii::$app->request->post())) {
return Json::encode(['msg' => '提交内容有误 !!']);
}
// 验证失败
if (!$model->validate()) {
return Json::encode($model->getErrors());
}
if (!($user = $model->userReg())) {
return Json::encode(['msg' => '注册失败,请检查 !!']);
}
if (!Yii::$app->getUser()->login($user, (3600 * 24 * 30))) {
return Json::encode(['msg' => '注册用户异常 !!']);
}
return Json::encode(['msg' => '注册成功 !!', 'status' => true]);
}
return $this->render('../center/reg', ['model' => $model]);
}
控制器里面的注册函数
奇怪的是我可以插入数据,但是插入的数据均为空白,我查过数据是有的,到save哪里不知道干嘛就成空白了..求指教...
yixianliu2 补充于 2017-09-22 16:03
INSERT INTO
F_user (
id) VALUES (DEFAULT)
看调试的时候,出现这个...
yixianliu2 补充于 2017-09-22 16:16
`
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public $user_id;
public $username;
public $password;
public $nickname;
public $telphone;
public $grade;
public $auth_key;
public $accessToken;
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::find()->where(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
/**
* 生成 "remember me" 认证key
*/
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* 为model的password_hash字段生成密码的hash值
*
* @param string $password
*/
public function setPassword($password)
{
return Yii::$app->security->generatePasswordHash($password);
}
/**
* 用户列表
*
* @return $this
*/
public function view()
{
return static::find()->Where(['is_using' => 'On']);
}
}
`
我的用户模型
共 5 个回答
-
没有rule规则吗?
赋值之后打印
$Cls看看有没有数据;
$Cls->save(false)//意思是不走rule验证规则?共 1 条回复yixianliu2 回复于 2017-09-22 17:01 回复rule规则放在了表单模型那边,打印过出来,都是有数据的
-
你声明的public属性,不要跟数据库中的重复,例如你数据库user表中有个“username”属性,那么在User中就不要在声明$username了
共 3 条回复yixianliu2 回复于 2017-09-23 08:42 回复不声明会出错啊..
yixianliu2 回复于 2017-09-24 08:50 回复@shuzhiqian 实现了..所以说好神奇
yixianliu2 广东深圳
注册时间:2016-12-11
最后登录:2024-10-12
在线时长:11小时22分
最后登录:2024-10-12
在线时长:11小时22分
- 粉丝0
- 金钱505
- 威望0
- 积分615