【问题标题】:yii2 how to make a notification by e-mail to the user after changing his statusyii2如何在更改状态后通过电子邮件通知用户
【发布时间】:2018-03-08 06:52:34
【问题描述】:

yii2 用户状态改变后如何通过邮件通知用户??请帮帮我!

型号

class Applicants extends \yii\db\ActiveRecord
{
    public $file;
    public static function tableName()
    {
        return 'applicants';
    }

    public function rules()
    {
        return [
            [['user_id', 'type_passport_id', 'passport_number', 'date', 'expiration_date', 'passport_path', 'diplom_number', 'diplom_path', 'phone_number', 'mobile_number', 'country_id', 'city_id', 'address', 'faculty_id', 'spec_id', 'stage_id', 'training_id', 'dormitories_id'], 'required'],
            [['user_id', 'type_passport_id', 'country_id', 'city_id', 'status_id', 'faculty_id', 'spec_id', 'stage_id', 'training_id', 'dormitories_id'], 'integer'],
            [['passport_number', 'mobile_number', 'applicants_comment'], 'string', 'max' => 250],
            [['expiration_date'], 'string', 'max' => 120],
            [['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf'],
            [['passport_path', 'diplom_path', 'address'], 'string', 'max' => 255],
            [['diplom_number'], 'string', 'max' => 50],
            [['phone_number'], 'string', 'max' => 30],
            [['user_id'], 'unique'],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

请帮帮我!

【问题讨论】:

  • 添加函数changeStatus($newStatus),用于改变用户状态,以及内部处理发送邮件。
  • 可以解释更多
  • 使用函数changeStatus($newStatus)(你需要写)来改变用户状态。在此功能中,您可以在保存用户状态后向用户发送电子邮件。
  • 嘿,答案是否为您解决了,如果对您有所帮助,请将其标记为已选正确

标签: email notifications yii2 status


【解决方案1】:

您可以使用afterSave($insert,$changedAttributes) 来检查更改的属性并发送电子邮件。

根据DOCS $changedAttributes 参数,attributes 的旧值已更改并已保存。您可以使用此参数根据所做的更改采取措施,例如在 password 发生更改时发送电子邮件或实施跟踪所有更改的 audit 跟踪。 $changedAttributes 为您提供旧的 attribute 值,而活动记录 ($this) 已经具有新的更新值。

public function afterSave( $insert , $changedAttributes ) {

    if ( !$insert && isset ( $changedAttributes['status'] ) ) {
        $this->sendEmail ();
    }
    return parent::afterSave ( $insert , $changedAttributes );
}

private function sendEmail() {
    //your code to email
}

【讨论】:

    【解决方案2】:
    public function afterSave($insert, $changedAttributes){
        if (!$insert && array_key_exists('status_id', $changedAttributes) && $this->status_id != $changedAttributes['status_id']){
            $this->sendEmail();
        }
        return parent::afterSave($insert, $changedAttributes);
    }
    
    private function sendEmail() {
        //your code to email
    }
    

    最好的检查方法是属性已更改afterSave()。请参阅this answer 中的Note

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2021-04-29
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多