在yii2中创建通知
问题描述:
我是yii的新手。我创建了两个表格,user
和notification
。表notification
具有userid
作为外键。我想创建针对用户在用户模型通知,就像我从用户模式在yii2中创建通知
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();
}
答
除了你的函数的名称的通知(应该是getNotifications()没有getnotifications()),我见你的代码没有错。
public function getNotifications()
{
return $this->hasMany(Notification::className(), ['user_id' => 'id']);
}
现在有什么问题?
我如何创建新通知? –
此功能不是创建新的通知。如果你想创建一个通知,你需要在你的控制器中建立一个新的Notification模型。请先阅读文档。 [这里](http://www.yiiframework.com/doc-2.0/guide-structure-models.html) – stfsngue