原文地址:http://www.it610.com/article/4918541.htm

行为就是继承yii\base\behavior,可以绑定到任意yii\base\compent实例上,然后这个compent实例就拥有了行为类所具有的属性和方法;

注意:Behavior只能与Component类绑定

参考出处:http://www.digpage.com/behavior.html

下面是两个例子:

1、分别定义行为类MyBehavior.php和组件类MyBehaviorAttachClass.php

(1)MyBehavior.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
namespace app\models;
use yii\base\Behavior;
?>
<?
class MyBehavior extends Behavior
{
    public $propertyTest='this is MyBehavior propertyTest';
    public function methodTest()
    {
        echo 'this is MyBehavior methodTest';
    }
 
}
?>

(2)MyBehaviorAttachClass.php

1
2
3
4
5
6
7
8
9
10
<?php
namespace app\models;
use yii\base\component;
?>
<?
class MyBehaviorAttachClass extends component
{
 
}
?>

(3)控制器中写个方法,以便演示时调用

1
2
3
4
5
6
7
8
public function actionBehavior()
    {
        $MyBehavior=new MyBehavior;
        $MyBehaviorAttachClass=new MyBehaviorAttachClass;
        $MyBehaviorAttachClass->attachBehavior('MyBehavior',$MyBehavior);
        echo $MyBehaviorAttachClass->propertyTest;
        echo $MyBehaviorAttachClass->methodTest();
    }

此时运行r=hello/Behavior就会显示如下界面:

2、在ActiveRecord中调用行为,这种属于静态调用,直接在类中写个behaviors方法就可以了

详情看:http://www.yiichina.com/question/807

下面是gii_test.php,位于models下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class gii_test extends \yii\db\ActiveRecord
{
    
public function behaviors()
{
return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],//其中created_at和updated_at是gii_test数据表的字段名,必须设置为int才能显示时间戳
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],//否则显示00000000
                ],
            ],
        ];
   }
}

如果需要保留字段属性为 timestamp ,可以使用如下方法自动填充:

1
2
3
4
5
6
7
8
9
10
11
12
use yii\db\Expression;
public function behaviors()
{
     return [
         [
             'class' => TimestampBehavior::className(),
             'createdAtAttribute' => 'created_at',
             'updatedAtAttribute' => 'updated_at',
             'value' => new Expression('NOW()'),
         ],
     ];
}

行为Behavior的使用的更多相关文章

  1. Yii2的深入学习--行为Behavior

    我们先来看下行为在 Yii2 中的使用,如下内容摘自 Yii2中文文档 行为是 [[yii\base\Behavior]] 或其子类的实例.行为,也称为 mixins,可以无须改变类继承关系即可增强一 ...

  2. 《Note --- Unreal 4 --- behavior tree》

    Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...

  3. 使用行为树(Behavior Tree)实现游戏AI

    ——————————————————————— 谈到游戏AI,很明显智能体拥有的知识条目越多,便显得更智能,但维护庞大数量的知识条目是个噩梦:使用有限状态机(FSM),分层有限状态机(HFSM),决策 ...

  4. Dynamics AX 2012 R2 AIF No Endpoint Behavior Named 'clientEndpointBehavior'

          最近,Reinhard在使用Http Adapter类型的AIF入站端口时,总是报以下错误: Server Error in '/MicrosoftDynamicsAXAif60' App ...

  5. Silverlight behavior(行为) trigger 大全

    behavior是超级有用的东西,一定要学会,因为这个就是面向对象编程中的封装.超级重要! 欢迎大家如果有好的效果,可以给我留言,我打算不断的整理这个behavior,希望不久用behavior可以做 ...

  6. 深入理解MVVM模式中Silverlight的Trigger、Action和Behavior及Silverlight的继承机制

    接触Silverlight已经有两三个月了,开始一直感觉他和Winform很相似,拖拖控件就行了,所以一直把经历放在了研究后台和服务器交互和性能优化上面,很少去仔细研究Silverlight的页面.前 ...

  7. silverlighter下MVVM模式中利用Behavior和TargetedTriggerAction实现文本框的一些特效

    在silverlight一般开发模式中,给文本框添加一些事件是轻而易举的,然而MVVM开发模式中,想要给文本框添加一些事件并非那么容易,因为MVVM模式中,只有ICommand接口,而且也只有Butt ...

  8. 十五天精通WCF——第九天 高级玩法之自定义Behavior

    终于我又看完了二期爱情保卫战,太酸爽了,推荐链接:http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd,不多说,谁看谁入迷,下面言归正传, 看看这 ...

  9. 自定义CoordinatorLayout Behavior 隐藏Footer View

    在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...

  10. Behavior Tree

    http://www.craft.ai/blog/bt-101-behavior-trees-grammar-basics/ https://github.com/libgdx/gdx-ai/wiki ...

随机推荐

  1. LUA __call

    1. ev={} . functin ev.__call() . print "called from ev" . end . . setmetatable(ev, ev) . . ...

  2. [转]World Wind学习总结一

    WW的纹理,DEM数据,及LOD模型 以earth为例 1. 地形数据: 默认浏览器纹理数据存放在/Cache/Earth/Images/NASA Landsat Imagery/NLT Landsa ...

  3. Wifi开发技术总结1

    摘要: 刚刚接触wifi开发的东西,用的模块是 ESP8266-12E. 资料很多,淘宝地址:https://item.taobao.com/item.htm?spm=a1z09.2.9.10.qGL ...

  4. memcached工作原理与优化建议

    申明,本文为转载文:http://my.oschina.net/liuxd/blog/63129 工作原理 基本概念:slab,page,chunk. slab,是一个逻辑概念.它是在启动memcac ...

  5. 给ubuntu开通FTP功能

    一.安装vsftp安装: sudo apt-get install vsftpd 二.启动.停止.重启vsftp 启动vsftp:sudo service vsftpd start 三.创建ftp用户 ...

  6. HoloLens开发手记 - Unity之Locatable camera 使用相机

    Enabling the capability for Photo Video Camera 启用相机能力 为了使用摄像头,我们必须启用WebCam能力. 在Unity中打开Player settin ...

  7. 分页pagination实现及其应用

    1.分页jquery.page.js //分页插件 /** 2014-08-05 ch **/ (function ($) { var ms = { init: function (obj, args ...

  8. [BZOJ1951][SDOI2005]古代猪文(数论好题)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1951 分析: 练习数论知识的好题,涉及到费马小定理.lucas定理.求逆元

  9. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

  10. 02.C#可空類型、默認參數、LINQ(一章1.3-1.4)

    利用上班時間發個隨筆,不知領導會不會看到,可能會有同事看到也說不定啊:) 關于可空類型,在C#1中沒有這個概念,在C#3中引入的.那比如我們要實現一個表示人的類,人有名字和年齡兩個屬性,如何表示一個沒 ...