Okay, so far, attaching the behavior statically works, as does attaching it dynamically right after model initialization.
The configuration looks like this:
$user = new User();
$user->attachBehavior( 'test',[
'class' => loyaltyBehavior::className(),
'userActions' => [
User::EVENT_AFTER_INSERT => [
'user_registered'=> [
'content' => 'username'
]
]
]]);
So I tried attaching them from a db saved configuration by bootstrapping my module. The function in my module init:
public function init()
{
parent::init();
// custom initialization code goes here
if (isset($this->params['db_config']) && $this->params['db_config']>0) {
\Yii::trace('db_config > 0');
$configs = ConfigTable::find()->all();
$i=0;
foreach ($configs as $cfg){
\Yii::trace('found config');
$class = $cfg->class;
$event = $cfg->event;
$actionName = $cfg->action_name;
$behaviorName = 'loyaltyBehavior'.$i;
$attributes = empty($cfg->attr) ? '' : explode(',',$cfg->attr);
$content = empty($cfg->content) ? '' : explode(',',$cfg->content);
$behaviorConfig =[
'class' => loyaltyBehavior::className(),
'userActions' => [
constant($class.'::'.$event) => [
$actionName => [
'attributes' =>$attributes,
'content' => $content,
]
]
]];
\Yii::trace(var_export($behaviorConfig,true));
$class::attachBehavior($behaviorName, $behaviorConfig);
$i++;
}
}
}
The Yii:traces fire and the configuration looks good but the event doesn’t fire.
After that I tried to attach by config:
'userModel' =>[
'class' => 'common\models\User',
'as myBehavior1' => [
'class' => 'common\\modules\\loyalty\\components\\loyaltyBehavior',
'userActions' => [
'afterInsert' => [
'user_registered'=> [
'content' => 'username'
]
]
]
]],
And it still doesn’t work.
I’m pretty stuck at this point, I could really use some veteran help~