怎么在yii中利用model添加默认值-创新互联

这篇文章将为大家详细讲解有关怎么在yii中利用model添加默认值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联公司专注于金秀企业网站建设,自适应网站建设,商城建设。金秀网站建设公司,为金秀等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

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,否则不会保存。

关于怎么在yii中利用model添加默认值就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章题目:怎么在yii中利用model添加默认值-创新互联
当前网址:http://pwwzsj.com/article/dhsgij.html