這篇文章主要講解了“PHP yii怎么實現model添加默認值”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“PHP yii怎么實現model添加默認值”吧!
yii實現model添加默認值的方法(2種方法)
本文實例講述了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,否則不會保存。
感謝各位的閱讀,以上就是“PHP yii怎么實現model添加默認值”的內容了,經過本文的學習后,相信大家對PHP yii怎么實現model添加默認值這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創新互聯網站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!
當前標題:PHPyii怎么實現model添加默認值-創新互聯
當前網址:http://vcdvsql.cn/article10/ccsddo.html
成都網站建設公司_創新互聯,為您提供網站設計公司、響應式網站、軟件開發、外貿建站、品牌網站制作、Google
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯