【发布时间】:2026-02-19 00:55:01
【问题描述】:
这是一个与this one 非常相似的问题,但表之间的关系不同。
我有 3 个表:users、notifications 和 user_notifications。 user_notifications 是用户和通知之间的连接表,表示已发送给用户的通知实例。
关系如下:
User => hasMany => UserNotification
UserNotification => belong to => User, Notification
Notification => hasMany => UserNotification
各栏目如下:
用户 => id、姓名
UserNotification => id, user_id, notification_id
通知 => id,消息
我想创建一个名为 Notification.users 的虚拟字段,它只包含一个字符串列表,其中包含已发送该特定通知的所有用户,例如:
User => id = 1, name = Bob
User => id = 2, name = Bill
UserNotification => id = 1, user_id = 1, notification_id = 1
UserNotification => id = 2, user_id = 2, notification_id = 1
Notification => id = 1, message = "Hello World!"
所以通知 1,“Hello World!”已发送给用户 1 和 2、Bob 和 Bill。因此,虚拟字段Notification.users 包含这两个名称的逗号分隔列表,我看到了:
Notification => id = 1, message = "Hello World!", users = "Bob,Bill"
【问题讨论】:
标签: php postgresql cakephp virtual