这篇文章主要介绍了yii实现model添加默认值的方法,结合实例分析了在rules()方法及在beforesave()方法中设定两种实现技巧,需要的朋友可以参考下
本文实例讲述了yii实现model添加默认值的方法。分享给大家供大家参考,具体如下:
yii model 继承自CActiveRecord
有些字段可能不会出现在表单中,而需要在程序中加入。如订单编号,时间戳,操作的user_id等等。
以下二种方法:
1、在rules()方法中设定:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('start, end', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('timestamp','default','value'=>date('Y-m-d H:i:s')),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, start, end, user_id, timestamp', 'safe', 'on'=>'search'),
);
}
2、在beforeSave()方法中设定:
程序介绍: 《快打折淘宝客折扣系统V2.5 免API采集》是一款适用于淘宝客打折单品推广的程序,无论老手或新手都能短时间内赚取大量佣金的淘宝客网站程序,不同于现在广泛的淘宝客单品推广网站。本程序可手动添加商品,同时也配置强大的多功能采集插件,多种采集方案,内置9.9元、19.9元包邮采集规则,可采集评论,也可自定义采集规则,全自动无人值守采集更新网站,无需人工维护。默认提供精美的页面设计模版,超好
function beforeSave()
{
$this->user_id = Yii::app()->user->id;
return true;
}
需要注意的是,beforeSave()方法需要return true,否则不会保存。
相关推荐:









