yii实现model添加默认值的方法(2种方法),yiimodel
本文实例讲述了yii实现model添加默认值的方法。分享给大家供大家参考,具体如下:
yii model 继承自CActiveRecord
有些字段可能不会出现在表单中,而需要在程序中加入。如订单编号,时间戳,操作的user_id等等。
以下二种方法:
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框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- Yii基于数组和对象的Model查询技巧实例详解
- Yii中Model(模型)的创建及使用方法
- PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
- Yii不依赖Model的表单生成器用法实例

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.