“之后的电子邮件通知”;在“;命令是针对yii2中的特定表触发的

“之后的电子邮件通知”;在“;命令是针对yii2中的特定表触发的,yii2,Yii2,我试图找到一种方法,在kf_notifications表中更新记录时实现电子邮件通知功能。电子邮件通知应发送到与新更新记录中的nofications表中的用户id对应的电子邮件地址。电子邮件ID存储在另一个名为kf\u users 控制器: public function actionNotification() { $uid = Yii::$app->user->identity->id; if(isset($_GET['new'])) {

我试图找到一种方法,在
kf_notifications
表中更新记录时实现电子邮件通知功能。电子邮件通知应发送到与新更新记录中的nofications表中的
用户id
对应的电子邮件地址。电子邮件ID存储在另一个名为
kf\u users

控制器:

 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]);
}
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],
    ];
}
kf\U通知(型号):

 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]);
}
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],
    ];
}

你的题目和问题的主体问了两个不同的问题?您询问的是哪一个问题,请发送电子邮件至
update
insert
?无论如何,我假设您希望它位于记录/行的
更新

您可以在模型
kf_通知
内覆盖
afterSave()
,它需要
$insert
$changedAttributes
保存布尔值的参数(对于
$insert
true
false
,具体取决于执行的是
插入
还是
更新
操作)

我假设您有发送电子邮件的代码,因此我将添加覆盖
afterSave()
的方法,并使用
$this->senEmail()
作为电子邮件函数的引用

如果您在
kf\u通知
表中添加一个关系(如下所示)会更好

public function getUser() {
    return $this->hasOne(User::class, ['id' => 'user_id']);
}
注意:如果用户的模型名与其他名称同名且名称空间完整,请更新该模型名

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 (!$insert && !empty($changedAttributes)) {


如果答案对你有效,请务必将其标记为正确。我一定会这样做,同时请分享发送电子邮件的代码。谢谢。您可以在yiiDocs上轻松找到它,它为您提供了设置和配置
SwiftMailer
的所有信息。此外,您还可以看到这一点。