《PHP教程:yii實現model添加默認值的方法(2種方法)》要點:
本文介紹了PHP教程:yii實現model添加默認值的方法(2種方法),希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
本文實例講述了yii實現model添加默認值的辦法.分享給大家供大家參考,具體如下:PHP學習
yii model 繼承自CActiveRecordPHP學習
有些字段可能不會出現在表單中,而需要在程序中加入.如訂單編號,時間戳,操作的user_id等等.PHP學習
以下二種辦法:PHP學習
1、在rules()辦法中設定:PHP學習
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()辦法中設定:PHP學習
function beforeSave() { $this->user_id = Yii::app()->user->id; return true; }
需要注意的是,beforeSave()辦法需要return true,否則不會保存.PHP學習
希望本文所述對大家基于Yii框架的PHP程序設計有所贊助.PHP學習
《PHP教程:yii實現model添加默認值的方法(2種方法)》是否對您有啟發,歡迎查看更多與《PHP教程:yii實現model添加默認值的方法(2種方法)》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7798.html