YII Behavior重用

转载自:http://hudeyong926.iteye.com/blog/1483828

用behavior的好处是可以通过“插入”的方式来获得新的功能。你当然可以直接把代码写在model里。不过如果类似的代码需要在若干个model里实现,那么behavior就可以让你重用这段代码 。

 

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

Java代码   收藏代码
  1. public function behaviors(){  
  2.     return array(  
  3.         'CTimestampBehavior' => array(  
  4.             'class' => 'zii.behaviors.CTimestampBehavior',  
  5.             'createAttribute' => 'create_time_attribute',  
  6.             'updateAttribute' => 'update_time_attribute',  
  7.         )  
  8.     );  
  9. }  
 

XSS安全模式类

在这篇文章里,我们将描述一个基于WEB应用下避免不合法的内容注入。

我们要在一个行为里使用htmlpurifier 类,用这种行为可以加强任何模型并表明各属性我们想让它们XSS安全。

我写了以下行为:

Java代码   收藏代码
  1. <?php  
  2. class CSafeContentBehavior extends CActiveRecordBehavior  
  3. {  
  4.     public $attributes =array();  
  5.     protected $purifier;  
  6.   
  7.     function __construct(){  
  8.         $this->purifier = new CHtmlPurifier;  
  9.     }  
  10.   
  11.     public function beforeSave($event)  
  12.     {  
  13.         foreach($this->attributes as $attribute){  
  14.             $this->getOwner()->{$attribute} = $this->purifier->purify($this->getOwner()->{$attribute});  
  15.         }  
  16.     }  
  17. }  

把这个类放在你的应用程序目录,例如:application/behaviors/CSafeContentBehavior.php。现在你在模型的行为中这样去写:

Java代码   收藏代码
  1. <?php  
  2. class Post extends CActiveRecord  
  3. {  
  4.     public function behaviors(){  
  5.         return array(  
  6.             'CSafeContentBehavor' => array(   
  7.                 'class' => 'application.behaviors.CSafeContentBehavior',  
  8.                 'attributes' => array('title''body'),  
  9.             ),  
  10.         );  
  11.     }  
  12. }  

现在我们可以开始了。我们的post模型在每个保存操作中将净化标题和内容列。

 

保存一条记录后,更新订单号,适合所有订单号

Java代码   收藏代码
  1. <?php  
  2. class No13Behavior extends CActiveRecordBehavior  
  3. {  
  4.     public $pk = '';  
  5.     public $orderNo = '';  
  6.     public $prefix = '';  
  7.       
  8.     public function afterSave($event)  
  9.     {  
  10.         if ($this->getOwner()->getIsNewRecord()){  
  11.             if(empty($this->pk)||empty($this->orderNo)||empty($this->prefix)){  
  12.                 return false;  
  13.             }  
  14.             $id = $this->getOwner()->{$this->pk};  
  15.             $model = $this->getOwner()->findByPk($id);  
  16.             $model->{$this->orderNo} = $this->prefix . date('ymd') . str_pad($id, 5'0', STR_PAD_LEFT);  
  17.             $model->save();  
  18.         }  
  19.     }  
  20. }  

  自动导入module模块

Java代码   收藏代码
  1. <?php  
  2. /** 
  3.  * ApplicationConfigBehavior is a behavior for the application. 
  4.  * It loads additional config parameters that cannot be statically  
  5.  * written in config/main 
  6.  */  
  7. class ApplicationConfigBehavior extends CBehavior  
  8. {  
  9.     /** 
  10.      * Declares events and the event handler methods 
  11.      * See yii documentation on behavior 
  12.      */  
  13.     public function events()  
  14.     {  
  15.         return array_merge(parent::events(), array(  
  16.             'onBeginRequest'=>'beginRequest',  
  17.         ));  
  18.     }  
  19.    
  20.     /** 
  21.      * Load configuration that cannot be put in config/main 
  22.      */  
  23.     public function beginRequest()  
  24.     {  
  25.         $modules = array();  
  26.         $model = Module::model()->findAll(); // Todo - should be per application  
  27.         foreach ($model as $item)  
  28.         {  
  29.             $modules[$item->name] = array();// Todo can set parameters here for each module...   
  30.         }         
  31.         //$modules['video'] = array();  
  32.         Yii::app()->setModules($modules);  
  33.     }  
  34. }  
  35. ?>  
 
Main.php代码   收藏代码
  1. 'behaviors' => array('application.components.behaviors.ApplicationConfigBehavior'),  
  2. ;  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值