這篇文章將為大家詳細講解有關怎么在yii中利用model添加默認值,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
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中利用model添加默認值就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。