《PHP編程:PHP yii實(shí)現(xiàn)model添加默認(rèn)值的方法(兩種方法)》要點(diǎn):
本文介紹了PHP編程:PHP yii實(shí)現(xiàn)model添加默認(rèn)值的方法(兩種方法),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
yii實(shí)現(xiàn)model添加默認(rèn)值的方法(2種方法)
PHP實(shí)戰(zhàn)
這篇文章主要介紹了yii實(shí)現(xiàn)model添加默認(rèn)值的方法,結(jié)合實(shí)例分析了在rules()方法及在beforeSave()方法中設(shè)定兩種實(shí)現(xiàn)技巧,對(duì)大家也許有幫助,PHP實(shí)戰(zhàn)
本文實(shí)例講述了yii實(shí)現(xiàn)model添加默認(rèn)值的方法.分享給大家供大家參考,具體如下:PHP實(shí)戰(zhàn)
yii model 繼承自CActiveRecordPHP實(shí)戰(zhàn)
有些字段可能不會(huì)出現(xiàn)在表單中,而需要在程序中加入.如訂單編號(hào),時(shí)間戳,操作的user_id等等.PHP實(shí)戰(zhàn)
以下二種方法:PHP實(shí)戰(zhàn)
1、在rules()方法中設(shè)定:PHP實(shí)戰(zhàn)
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()方法中設(shè)定:PHP實(shí)戰(zhàn)
function beforeSave() { $this->user_id = Yii::app()->user->id; return true; }
需要注意的是,beforeSave()方法需要return true,否則不會(huì)保存.PHP實(shí)戰(zhàn)
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助.PHP實(shí)戰(zhàn)
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2837.html