时间模型类TimestampBehavior

来源:互联网 发布:古生物学与地层学知乎 编辑:程序博客网 时间:2024/06/08 17:21

概念概况

当模型发生变动时,自动给模型赋值时间

用法:
在模型类里加上以下方法即可

use yii\behaviors\TimestampBehavior;use yii\db\ActiveRecord;class CarSuit extends ActiveRecord{    public function behaviors()    {        return [            [                'class' => TimestampBehavior::className(),                'createdAtAttribute' => 'createtime',                'updatedAtAttribute' => 'updatetime',                'attributes' => [                    ActiveRecord::EVENT_BEFORE_INSERT => ['createtime', 'updatetime'],                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updatetime'],                ]            ]        ];    } ... }

以上当CarSuit这个类模型发生变动,createtimeupdatetime 也会变化.

就这么简单…

原创粉丝点击