Home >Backend Development >PHP Tutorial >yii implements the method of adding default value to model (2 methods), yiimodel_PHP tutorial

yii implements the method of adding default value to model (2 methods), yiimodel_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:261167browse

yii implements the method of adding default value to model (2 methods), yiimodel

This article describes the example of yii implementing the method of adding default value to model. Share it with everyone for your reference, the details are as follows:

yii model inherits from CActiveRecord

Some fields may not appear in the form and need to be added in the program. Such as order number, timestamp, user_id of operation, etc.

The following two methods:

1. Set in the rules() method:

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. Set in the beforeSave() method:

function beforeSave()
{
  $this->user_id = Yii::app()->user->id;
  return true;
}

It should be noted that the beforeSave() method needs to return true, otherwise will not be saved.

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Articles you may be interested in:

  • Detailed examples of Yii’s Model query techniques based on arrays and objects
  • How to create and use Model (model) in Yii
  • PHP YII framework development tips: custom validation rules for rules in models
  • Yii form generator usage examples that do not rely on Model

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089200.htmlTechArticleYii implements the method of adding default value to model (2 methods), yiimodel This article describes the example of yii implementing model to add default value value method. Share it with everyone for your reference, the details are as follows: yii mod...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn