我想创建一个通知系统。有一个Notification
课。通知可以分配给多个用户,而不仅仅是一个。
有一个联合表user_notifications
,有两列:user_id
和notification_id
$notifications
用户类中的定义是这样的:
/**
* @ManyToMany(targetEntity="Notification")
* @JoinTable(name="user_notifications",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="notification_id", referencedColumnName="id", unique=true)}
* )
**/
private $notifications;
一切正常。但是user_notifications
,无论通知是否由给定的用户读取,我都想在表中添加一个新列,并将其存储在该表中。我应该如何在教义2中管理它?
您将必须重构您的实体以引入一个新实体,并将user_notifications
邻接表转换为一个实体。
解决方案
按如下所示变换您的表:
然后重构您的关联,如下所示:
用户实体
...
/**
* @OneToMany(targetEntity="UserNotification", mappedBy="notification_id")
**/
private $notifications;
通知实体
...
/**
* @OneToMany(targetEntity="UserNotification", mappedBy="user_id")
**/
private $users;
用户通知实体
/** @Entity **/
class UserNotification {
...
/**
* @ManyToOne(targetEntity="User", inversedBy="notifications")
* @JoinColumn(name="user_id", referencedColumnName="id")
**/
private $user_id;
/**
* @ManyToOne(targetEntity="Notification", inversedBy="users")
* @JoinColumn(name="notification_id", referencedColumnName="id")
**/
private $notification_id;
/** @Column(type="boolean") */
private $read;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句