CActiveRecordBehavior missing init() ?

在开发行为插件时,遇到一个问题~

找遍了 CActiveRecordBehavoir, CModelBehavior, CBehavior

也没有看到 init() 初始化插件的接口~

这样的话,我就没有办法利用插件来添加一个“固定”关联~

按目前的机制来看,必须使用一个方法,才可以触发插件发生作用,如:

Post::model()->localized()->findAll();

localized() 是插件方法,会动态增加关联~ 可这并不满足于我开发行为插件的需求~

大P帮助下,发现了 afterConstruct() 方法,是CActiveRecordBehavior独有的~赞!

贴一段ActiveRecord构造的代码~


public function __construct($scenario='insert')

{

    if($scenario===null) // internally used by populateRecord() and model()

        return;

    $this->setScenario($scenario);

    $this->setIsNewRecord(true);

    $this->_attributes=$this->getMetaData()->attributeDefaults;

    $this->init();

    $this->attachBehaviors($this->behaviors());

    $this->afterConstruct();

}



发现了一个问题~

只有当 new Post; 的时候, afterConstruct() 才会被触发~

而当 $post = Post::model()->find(); 时,没有触发~

这样不统一啊~ 是否bug?

查看上面所贴的源码可以看到,假如 scenario 为 null,会直接return 跳过了后面的函数执行~

建议增加一个事件:

[color="#2E8B57"]afterAttachBehaviors()[/color]

afterFind和afterConstruct分别用于find和new的实例。

是否能够提供一个 afterBindBehavior() 事件呢?

如,我想做一个RecycleBinBehavior插件,我希望在模型配置了这个插件,

这个插件即可以马上工作~

$posts = Posts::model()->findAll();

我希望上面的查询会给绑上一个查询条件为 is_recycled=0 ~