[Yii Framework] 创建自己的extension

创建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
)
);
}

原文地址:https://www.cnblogs.com/davidhhuan/p/1717035.html