This is a very similar question to this one, but with a different relationship between the tables.
I have 3 tables: users
, notifications
, and user_notifications
. user_notifications
is a join table between users and notifications, and represents an instance of a notification having been sent to a user.
The relations are as follows:
User => hasMany => UserNotification
UserNotification => belong to => User, Notification
Notification => hasMany => UserNotification
The columns are as follows:
User => id, name
UserNotification => id, user_id, notification_id
Notification => id, message
I wanted to create a virtual field called Notification.users
that simply held a string list of all the users that had been sent that particular notification, for example:
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!"
So notification 1, "Hello World!" has been sent to users 1 and 2, Bob and Bill. Therefore, the virtual field Notification.users
contains a comma-separated list of those two names, and I see:
Notification => id = 1, message = "Hello World!", users = "Bob,Bill"