Help with creating custom events

I am creating an app and I’d like to expand my coding abilities by starting to use custom events, but I’ve been having trouble configuring them correctly and getting them to fire. My example is that this site has a Membership which is an ActiveRecord object and a membership can become activated, at which point I’d like to fire off an after activation event. This event will send out emails, and also potentially fire off other events such as subscribing to a journal if they have chosen to do so. I’d like this event to be permanently tied to the model.

Here is what I have in my Membership model so far in attempting to accomplish this:




namespace common\models;


use \common\components\MembershipHelper;

use Yii;

use \yii\db\ActiveRecord;


class Membership extends ActiveRecord

{

    /**

     * @const string Name of the activation event for a membership being activated

     */

    const EVENT_AFTER_ACTIVATION = 'afterActivation';

....

    /**

     * @inheritdoc

     */

    public function afterSave($insert, $changed_attributes)

    {

        if($insert && $this->active == 1 || array_key_exists('active', $changed_attributes) && $this->active == 1){

            $this->trigger(self::EVENT_AFTER_ACTIVATION);

        }

        return parent::afterSave($insert, $changed_attributes);

    }


    /**

     * Event raised after a membership is activated

     * @return void

     */

    public function afterActivation(){

        if($this->isNewRecord && $this->jppid = 1){

            MembershipHelper::sendJournalEmail($this);

        }

        exit('we have just activated');

    }

}



I’ve read this (https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-events.md) and looked attaching event handlers. The code in the first box on that page looks like I’d have to attach the event handler each time in the controller after I instantiate that object. I would like them always attached, so I figure to do this and avoid attaching them every time I’d want to include attach them by overriding the ‘init’ function on the model to attach them.

I also read the reference there that I could do them using configurations, so I read up on this page (https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-configurations.md). Using the example, the only way I could get it to work was by adding some code like this:




'membership' => [

    'class' => '\common\models\Membership',

    'year' => 100000,

    'on afterActivation' => ['\common\models\Membership', 'afterActivation'],

]



under the ‘components’ key in my configuration array. However, this event is not attached with I attempt to instantiate a ‘new Membership’ or when I try to access a membership model that has been loaded, only when I access it through the Yii::$app->membership method. I attempted to configure the model to do it without using the ‘membership’ key and just using an unkeyed array in the configuration, and that did not work.

So, I am wondering, is my best bet for attaching these events using my first method described above? Or, when I create a new Membership model or load a Membership model should I always do it into the Yii::$app->membership method? Or, since a membership is related to a user, should I try to further configure the user component in my configuration with a ‘membership’ sub-component and access them that way? Or is there some other way to permanently attach events to a model that I should be using? I’ve thought about creating a behavior, but it doesn’t seem like this is the best method for me to use since this behavior is specific to the model.

Let me know if I can provide any more information, thanks!