The example of this article describes the method of adding the default value of YII implementation model. Share to everyone for your reference, specific as follows:
Yii model inherits from Cactiverecord
Some fields may not appear in the form, but need to be added to the program . such as order number, time stamp, Operation user_id and so on.
Here are two ways to:
1, in the Rules () method set:
Public function rules ()
{
//note:you should only define the rules for those attributes that
//would receive user Inputs.
Return Array (
' Start, end ', ' required '),
array (' user_id ', ' numerical ', ' integeronly ' =>true),
Array (' timestamp ', ' Default ', ' Value ' =>date (' y-m-d h:i:s ')),
//The following rule are used by search ().
Please remove the those attributes that should is searched.
Array (' ID, start, end, user_id, timestamp ', ' safe ', ' on ' => ' search ')
;
}
2, in the BeforeSave () method set:
function BeforeSave ()
{
$this->user_id = Yii::app ()->user->id;
return true;
}
It should be noted that theBeforeSave () method requires return true, otherwise it will not be saved .
I hope this article will help you with the PHP program design based on the YII framework.