yii实现model添加默认值的方法(2种方法)

以下二种方法:

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,否则不会保存

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/74077b14443c6cfd534beccf7402a683.html