在yii2中创建通知

在yii2中创建通知,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,我是yii的新手。我创建了两个表,user和notification。表notification将userid作为外键。我想在用户模型中创建针对用户的通知,就像从用户模型中获取通知一样 public function getnotifications() { return $this->hasMany(Notification::className(), ['user_id' => 'id']); } 除了函数名(应该是getNotifications()而不是getNo

我是yii的新手。我创建了两个表,
user
notification
。表
notification
userid
作为外键。我想在用户模型中创建针对用户的通知,就像从用户模型中获取通知一样

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

除了函数名(应该是getNotifications()而不是getNotifications()),我看不出代码中有任何错误

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();
}

在模型中使用此功能

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

如何创建新通知?此功能不是创建新通知。如果要创建通知,需要在控制器中构建新通知的模型。请先去看一下文件。如何创建新通知?此功能不是创建新通知。如果要创建通知,需要在控制器中构建新通知的模型。请先去看一下文件。