The plugin I've created has two tables with these structures :
CREATE TABLE notifications (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`sent_at` DATETIME NULL DEFAULT NULL,
`title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`message` TEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
`photo` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`icon` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci'
)
CREATE TABLE notification_recipients(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`notification_id` INT(11) NOT NULL,
`user_id` INT(11) NOT NULL,
`seen` TINYINT(1) NOT NULL,
`archived` TINYINT(1) NOT NULL
)
I can't figure out how to create a relation to show a backend select field where I can select users to get the notification, what should be done in the model.
I will later process the form in the controller in order to create the notification_recipients
line to every user selected.
After reading and watching the tutorial about relations, I've come to understand I might need a pivot table.
But I need the notification_recipients
table in order to process the notification later to show custom queries in frontend (seen
and archived
fields will determine the visibility in frontend).
How can I do that ? I don't understand the relationship I'm trying to build, shouw I have a third table ?
Thank you, best regards.
Update (solution found) :
I added a Checkbox List named "users". According to the docs of OctoberCMS, you can add options from the model to a Radio field, and Checkbox Lists work the same way.
For now, I got it working by doing this in the Model used for the form:
use RainLab\User\Models\User;
...
public function getUsersOptions($value, $formData)
{
$users = User::all();
$mapped = $users->mapWithKeys(function ($item) {
return [$item['id'] => $item['name']];
});
return $mapped;
}
It worked great and I'm getting all users with their ids.