由于AR类中的save函数是有两个参数的,第一个是验证是否打开,第二个是属性列表(默认为空)
所以如果更新或插入一条数据时,不符合你设定的rules要求,则调用save()不成功。
规则例子:
public function rules()
{
//
NOTE:
you should only define rules for those attributes that
//
will receive user inputs.
return array(
array('typeid, sideid, title, zaiyao, thumb, comment, addtime, uptime', 'required'),
array('typeid, sideid, addtime, uptime', 'numerical', 'integerOnly'=>true),
array('title, thumb', 'length', 'max'=>50),
array('zaiyao', 'length', 'max'=>200),
//
The following rule is used by search().
//
Please remove those attributes that should not be searched.
array('id, typeid, sideid, title, zaiyao, thumb, comment, addtime, uptime', 'safe', 'on'=>'search'),
);
}
调用:
$post1 = new News;
$post1->title = 'hello world';
$post1->comment =
'i am very good!';
$post1->save();
由于规则中设定所有字段都要required,所有当插入的时候只有两个字段,则插入失败;解决方法自然就有两个,要么按照你设置的要求插入数据,要么在调用save()时设置的第一个函数为false;
所以如果更新或插入一条数据时,不符合你设定的rules要求,则调用save()不成功。
规则例子:
调用:
由于规则中设定所有字段都要required,所有当插入的时候只有两个字段,则插入失败;解决方法自然就有两个,要么按照你设置的要求插入数据,要么在调用save()时设置的第一个函数为false;