在 yii2 中创建通知

create Notification in yii2

我是 yii 的新手。我创建了两个 table、usernotification。 table notificationuserid 作为外键。我想在用户模型中创建针对用户的通知,就像我从用户模型收到通知一样

 public function getnotifications()
{
    return $this->hasMany(Notification::className(), ['user_id' => 'id']);
}

除了函数的名称(应该是 getNotifications() 而不是 getnotifications() ),我看不出你有什么问题代码。

public function getNotifications()
{
   return $this->hasMany(Notification::className(), ['user_id' => 'id']);
}

现在是什么问题?

在你的模型中使用这个函数。

public function addNotification() {
    $notification = new Notification();
    $notification->user_id = $this->id;
    $notification->message = "Notification";
    $notification->save();
}