关于yii2.0中在调用model的save()方法时,数据库增加一行但是数据为空的问题
本文介绍了关于yii2.0中在调用model的save()方法时,数据库增加一行但是数据为空的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
在yii2.0中,我生成了一个model,并在这个model中设置了一个rules()方法并定义了相关规则。进行表单验证时,它会要求你先申明字段,否则报错。
但若是先申明字段的话,在调用save()方法存数据时会在数据库增加一个空行,若去掉声明,则可以正常执行。
现在要求这个model文件既可以做把表单验证,又可以正常的save().
有好的方法吗?
这个时model文件的代码
<?php
/**
* Created by BBM
* DateTime: 2016-11-14 15:51
*/
namespace wap\models;
use Yii;
use yii\db\ActiveRecord;
/**
* This is the model class for table "tsk_apply".
*
* @property integer $id
* @property integer $user_id
* @property string $pwd
* @property integer $status
* @property integer $admin_id
* @property integer $examine_date
* @property string $chinese_name
* @property string $identity_num
* @property string $mobile
* @property string $email
* @property string $nationality
* @property string $province
* @property string $city
* @property integer $sex
* @property integer $create_date
* @property string $remark
*
* @property UserModel[] $user
*/
class ApplyModel extends ActiveRecord
{
/**
* 在这里申明字段
*/
public $id;
public $user_id;
public $pwd;
public $status;
public $admin_id;
public $examine_date;
public $chinese_name;
public $identity_num;
public $mobile;
public $email;
public $nationality;
public $province;
public $city;
public $sex;
public $create_date;
public $remark;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%apply}}';
}
/**
* 验证规则
* @return array
*/
public function rules()
{
return [
['chinese_name','required','message'=>'姓名不能为空!'],
['identity_num','required','message'=>'身份证号不能为空!'],
['mobile','required','message'=>'手机号不能为空!'],
['email','required','message'=>'邮箱不能为空!'],
['nationality','required','message'=>'国籍不能为空!'],
['province','required','message'=>'省份不能为空!'],
['city','required','message'=>'城市不能为空!'],
['sex','required','message'=>'性别必须选择!'],
['email','email']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => '申请人ID',
'pwd' => '密码',
'status' => '申请状态',
'admin_id' => '审核人ID',
'examine_date' => '审核时间',
'chinese_name' => '中文名',
'identity_num' => '身份证号',
'mobile' => '手机号',
'email' => '邮箱',
'nationality' => '国籍',
'province' => '省份',
'city' => '城市',
'sex' => '性别',
'create_date' => '申请时间',
'remark' => '申请备注',
];
}
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if(parent::beforeSave($insert))
{
if($insert)
{
$this->create_date = time();
$this->status = 0;
}
else
{
$this->examine_date = time();
}
return true;
}
else
{
return false;
}
}
}
这个是添加数据的方法
public function addApply($params){
$model = new ApplyModel();
$model->user_id = $params['user_id'];
$model->pwd = $params['pwd'];
$model->chinese_name = $params['chinese_name'];
$model->identity_num = $params['identity_num'];
$model->mobile = $params['mobile'];
$model->email = $params['email'];
$model->nationality = $params['nationality'];
$model->province = $params['province'];
$model->city = $params['city'];
$model->sex = $params['sex'];
$result = $model->save();
return $result;
}
解决方案
Yii2里面不需要在model里面声明表字段作为属性;程序会自己从表结构里面获取字段作为属性值的。
所以你只需要将字段属性删除即可。
进行表单验证时,它会要求你先申明字段,否则报错。
除非你要验证的属性是不属于表字段的,就需要自己在model上添加
这篇关于关于yii2.0中在调用model的save()方法时,数据库增加一行但是数据为空的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文