sys2009 2012-02-18 11:20:16 4998次浏览 5条回复 2 0 0

转载自 http://zccst.iteye.com/blog/1338981 在yii中添加时间有两种方式

一、在beforesave()方法中设置

评价:相对麻烦

二、在../models/xx.php中增加时间戳代码 从Yii 1.1版本开始,Yii框架已经提供了一个CTimestampBehavior 行为类,只要设置好createAttribute和updateAttribute两个属性,,它分别对应你数据库表的创建时间和更新时间字段。像创建一篇文章时我们通常都会需要记录创建时间,更新时记录它的更新时间,详细使用,在你的Model类中behaviors 方法中增加下面几行, 将createAttribute和updateAttribute更改为你数据库对应的时间字段即可:

Php代码
// 添加时间戳

public function behaviors()   
{   
    return array(   
        'CTimestampBehavior'=>array(   
            'class' => 'zii.behaviors.CTimestampBehavior',   
            'updateAttribute' => 'updatetime',   //记录更新时刻的字段名
            //'updateAttribute' => null,   
            'createAttribute' => 'updatetime')   //记录新建时刻的字段名-可以另外设立个单独字段createtime
    );   
}
您需要登录后才可以回复。登录 | 立即注册