创建application component
继承IApplicationComponent接口,或者继承CApplicationComponent类,一定要覆盖IApplicationComponent::init()这个方法,因为component的实例化的时候要做一些准备工作的。

 

创建behavior
继承IBehavior接口,或者继承CBehavior类,
如果是为CModel或者CActiveRecord开发behaviors的话,可以继承CModelBehavior或者CActiveRecordBehavior,这些基类为CModel和CActiveRecord提供了额外的功能。
例如,继承CActiveRecordBehavior,在model里面使用这个behavior的时候,可以这个behavior可以影响到原来的CActiveRecord的基类方法的。如,


class TimestampBehavior extends CActiveRecordBehavior
{
public function beforeSave($event)
{
if($this->owner->isNewRecord)
$this->owner->create time=time();
else
$this->owner->update time=time();
}
}


在comment的model里面


Public function behaviors()
{
return array(
'AutoTimestampBehavior'=> array(
'class' => 'application.components.AutoTimestampBehavior',
//You can optionally set the field name options here
)
);
}

相关文章:

  • 2021-11-03
  • 2021-09-27
  • 2022-12-23
  • 2021-08-08
  • 2022-02-21
  • 2021-11-21
  • 2021-07-19
猜你喜欢
  • 2021-06-15
  • 2021-12-30
  • 2021-11-24
  • 2021-07-25
  • 2021-11-14
  • 2021-06-21
  • 2021-07-28
相关资源
相似解决方案