手机
当前位置:查字典教程网 >编程开发 >php教程 >yii实现model添加默认值的方法(2种方法)
yii实现model添加默认值的方法(2种方法)
摘要:本文实例讲述了yii实现model添加默认值的方法。分享给大家供大家参考,具体如下:yiimodel继承自CActiveRecord有些字段...

本文实例讲述了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()方法中设定:

function beforeSave() { $this->user_id = Yii::app()->user->id; return true; }

需要注意的是,beforeSave()方法需要return true,否则不会保存。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

【yii实现model添加默认值的方法(2种方法)】相关文章:

JavaScript实现滚动栏效果的方法

Yii实现自动加载类地图的方法

php实现修改新闻时删除图片的方法

php递归调用删除数组空值元素的方法

CodeIgniter针对lighttpd服务器URL重写的方法

php实现统计网站在线人数的方法

php简单实现快速排序的方法

用php实现gb2312和unicode间的编码转换

PHP实现多线程的两个方法

在Linux系统的服务器上隐藏PHP版本号的方法

精品推荐
分类导航