Skip to content

Instantly share code, notes, and snippets.

@ovicko
Last active May 19, 2021 11:09
Show Gist options
  • Save ovicko/f907d599a60ea89649f36f6a9d2a6b5d to your computer and use it in GitHub Desktop.
Save ovicko/f907d599a60ea89649f36f6a9d2a6b5d to your computer and use it in GitHub Desktop.
Add sendMail method to ActiveRecord class.
<?php
class Records extends \yii\db\ActiveRecord
{
 const EVENT_SEND_EMAIL_UPDATE = 'send-email-to-users';
 public function init() {
$this->on(self::EVENT_SEND_EMAIL_UPDATE, [$this, 'sendMail']);
//use this if you want to add custom data to your events
$this->on(self::EVENT_SEND_EMAIL_UPDATE, [$this, 'sendMail'],['Email 1','Email 2','Email 3']);
parent::init(); 
 }
 public function sendMail($event) {
//if you had attached some data you can use this to get the event data
$customData = $event->data;
 //send email to subscribers
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment