源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

yii实现model添加默认值的方法(2种方法)

  • 时间:2020-09-25 08:08 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:yii实现model添加默认值的方法(2种方法)
本文实例讲述了yii实现model添加默认值的方法。分享给大家供大家参考,具体如下: yii model 继承自CActiveRecord 有些字段可能不会出现在表单中,而需要在程序中加入。如订单编号,时间戳,操作的user_id等等。 以下二种方法: [b]1、在rules()方法中设定:[/b]
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'),
  );
}

[b]2、在beforeSave()方法中设定:[/b]
function beforeSave()
{
  $this->user_id = Yii::app()->user->id;
  return true;
}

需要注意的是,beforeSave()方法需要return true,否则不会保存。 希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部