Yii2 表单=>数据库时间戳存取转换
来源:互联网 发布:ubuntu top命令详解 编辑:程序博客网 时间:2024/06/07 10:50
- Model
public function behaviors(){ $behaviors = parent::behaviors(); $behaviors['convertTimestamp'] = [ 'class' => ConvertTimestampBehavior::className(), 'attributes' => [ static::EVENT_AFTER_FIND => ['birthday', 'employed_at', 'departure_at'], static::EVENT_BEFORE_INSERT => ['birthday', 'employed_at'], static::EVENT_BEFORE_UPDATE => ['birthday', 'employed_at', 'departure_at'], ], ]; return $behaviors;}public function rules(){ return [ ... [['birthday', 'employed_at', 'departure_at'], 'date', 'format' => 'php:Y-m-d'], ... ];}
- ConvertTimestampBehavior
use yii\base\Event;use yii\behaviors\AttributeBehavior;use yii\db\ActiveRecord;class ConvertTimestampBehavior extends AttributeBehavior{ public $dateFormat = 'Y-m-d'; /** * Evaluates the attribute value and assigns it to the current attributes. * @param Event $event */ public function evaluateAttributes($event) { if ($this->skipUpdateOnClean && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE && empty($this->owner->dirtyAttributes) ) { return; } if (!empty($this->attributes[$event->name])) { $attributes = (array) $this->attributes[$event->name]; foreach ($attributes as $attribute) { $value = $this->owner->$attribute; if ($event->name == ActiveRecord::EVENT_AFTER_FIND) { if (! is_int($value)) { $value = null; } else { $value = date($this->dateFormat, $value); } } else { if (is_int($value)) { continue; } $value = strtotime($value); } $this->owner->$attribute = $value; } } }}
阅读全文
0 0
- Yii2 表单=>数据库时间戳存取转换
- Yii2中,向数据库传送int类型的时间戳格式转换
- 数据库时间存取
- yii2.0使用AR查询,将数据库时间戳格式化
- yii2的表单验证与数据库操作
- Yii2 表单
- Yii2 表单
- YII2表单
- yii2表单
- yii2 时间
- Yii2.0 session的存取
- DEDECMS自定义表单时间戳转换成常规时间方法及添加时间方法
- 字符串时间转换数据库时间
- 数据库日期转换为时间戳
- 数据库时间格式转换
- 数据库时间格式转换
- 数据库时间转换
- Sql查询数据库时间戳与时间的相互转换
- FreeMarker整合到Spring中
- python aiohttp简易使用教程
- 13、Android开发基础之数据存储-查看保存的数据
- Angular 路由
- C# 深拷贝源码
- Yii2 表单=>数据库时间戳存取转换
- JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别
- 权限被拒之后,手动开启权限!
- 初学python------pdb调试命令
- Thinking in Java:第二章-一切都是对象
- thinkphp中eq标签的用法
- Codeforces Round #450 (Div. 2) E. Maximum Questions dp,重载小于号
- 用户填写提示文本框
- java工程师面试题:如何判断链表有环?