yii2 module类的behavior函数
来源:互联网 发布:拳皇咆哮 源码 编辑:程序博客网 时间:2024/05/16 06:36
之前已经说过了,函数behaviors在controller类中起的作用是进行拦截器的作用,而在Module中也是一样的
在yii2给出的例子是这样的:
/** * @inheritdoc */ public function behaviors() { return [ TimestampBehavior::className(), ]; }
而TimestampBehavior类是这样的
class TimestampBehavior extends AttributeBehavior{ public $createdAtAttribute = 'created_at'; public $updatedAtAttribute = 'updated_at'; public $value; /** * @inheritdoc */ public function init() { parent::init(); if (empty($this->attributes)) { $this->attributes = [ BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, ]; } } protected function getValue($event) { if ($this->value === null) { return time(); } return parent::getValue($event); } public function touch($attribute) { /* @var $owner BaseActiveRecord */ $owner = $this->owner; if ($owner->getIsNewRecord()) { throw new InvalidCallException('Updating the timestamp is not possible on a new record.'); } $owner->updateAttributes(array_fill_keys((array) $attribute, $this->getValue(null))); }}类TimestampBehavior是集成自AttributeBehavior,也就是说该拦截器主要作用时,当我们进行修改类的属性的时候触发的,而触发动作主要定义在函数 init当中的。
init函数需要填充一个数组该数组的每个元素都定义了一件事,就是当有INSERT数据库操作的时候,需要修改的类的属性的时候是 create_at 和update_at
当有UPDATE数据库操作的时候,需要重新修改的类的属性是update_at
实现的主要功能是函数init 和函数getValue
0 0
- yii2 module类的behavior函数
- yii2 controller behavior函数的beforeAction实现原理
- Yii2 property,behavior,event
- YII2 学习笔记 --- Behavior
- Yii2学习笔记(九):行为Behavior的使用
- yii2的rules()函数
- [源码学习] -- yii2源码学习笔记(六) -- Behavior和model类
- Behavior类
- [module] -- yii2中如何调用module
- yii2.0 mysql 函数的使用
- yii2模型中attributeLabels()函数的作用
- 深入理解Yii2.0 (3)行为(Behavior)
- yii2控制器中附加行为behavior()方法使用
- yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析
- Yii Behavior的理解
- 【笔记】Yii2 - Gii使用:Module Generator
- YII2 学习笔记 --- 创建模块 Module
- yii2添加module配置和常见问题解决
- 七种网卡绑定模式详解
- [模板]扩展KMP模板
- 关于UICollectionView的使用
- 开源WebGIS实施方案(一):开篇
- Javascript中获取时间new Date()详细介绍
- yii2 module类的behavior函数
- Scala变量定义val与var的区别
- 软件测试,ios与安卓的区别
- HTTP网络接口
- .net 调用JAVA短信接口
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
- PHP数据结构之九 PHP储存二叉树,二叉树的创建与二叉树的基本操作 遍历二叉树算法
- U3D学习笔记(2016年8月8日 )
- jqgrid--rownumbers添加标题头