-1

I'm trying to find a way to implement an email notification functionality whenever a record is updated in the kf_notifications table. The email notification should be sent to the email address that corresponds to the user_id in the nofications table withinin the new updated record. Email ID is stored in a different table named kf_users

Controller:

 public function actionNotification()
{
    $uid = Yii::$app->user->identity->id;

    if(isset($_GET['new'])) {
        //get new notifications
        $html = Yii::$app->Kiduchi->show_notification($uid);
    }
    else{
        //get all notifications
        $html = Yii::$app->Kiduchi->show_notification($uid,'all');
    }

    //update is_viewed
    $connection = \Yii::$app->db;
    $command = $connection->createCommand("UPDATE kf_notifications SET is_viewed=1 where user_id='$uid'");
    $command->execute();

    return $this->render('notification', ['html' => $html]);
}

kf_notification (model):

class Notifications extends \yii\db\ActiveRecord
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'kf_notifications';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['visible_all', 'user_id', 'text', 'is_viewed'], 'required'],
        [['user_id', 'is_viewed'], 'integer'],
        [['text'], 'string'],
        [['created_on'], 'safe'],
        [['visible_all'], 'string', 'max' => 255],
    ];
}
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Jontez
  • 9
  • 1
  • do mark the answer as correct if it worked for you – Muhammad Omer Aslam Sep 08 '18 at 12:32
  • i will definately do so, please also share the code to send the email. Thank you – Jontez Sep 11 '18 at 12:00
  • well, that can be easily found on the yiiDocs [here](https://www.yiiframework.com/doc/guide/2.0/en/tutorial-mailing) and provides you all the information to set up and configure the `SwiftMailer` moreover you can see this [answer](https://stackoverflow.com/questions/24995620/how-to-use-the-swiftmailer-in-yii2) that does the same. – Muhammad Omer Aslam Sep 11 '18 at 12:11

1 Answers1

0

Your title of the question and the body of the question are asking 2 different things? which one you are asking, send email on update or insert? Anyway, I assume that you want it on update of a record/row.

You can override afterSave() inside the model kf_notifications, it takes $insert and $changedAttributes parameters which hold boolean (for $insert | true or false depending if it is insert or update operation performed).

i assume that you have the code to send the email, so I will add the method to override the afterSave() and use $this->senEmail() as the reference to your email function.

It would be better if you add a relation inside your kf_notifications table like below.

public function getUser() {
    return $this->hasOne(User::class, ['id' => 'user_id']);
}

Note: please update the model name for the User if it is with some other name with complete namespace

public function afterSave($insert, $changedAttributes) {
    parent::afterSave($insert, $changedAttributes);

     //if it as update and some attributes were actually changed 
    //then send email to the user

    if (!$insert && !empty($changedAttributes)) {

        $userEmail=$this->user->email;
        $subject='Notification';
        $message=$this->text;

        //send email 
        $this->sendEmail($userEmail,$subject,$message);
    }
}

If you want to send on insert then change the check in the above code from

if (!$insert && !empty($changedAttributes)) {

to

if ($insert) {
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68