Search code examples
many-to-manyoctobercms

Add the same link multiple times in the pivot table in October CMS


I have two tables, having many-to-many relationship between them (user, shop) and a pivot table (visit) that links them together:

table user {
    id,
    name
}

table shop {
    id,
    name
}

table visit {
    id,
    user_id,
    shop_id,
    date
}

On the user's backend page, I'd like to be able to add multiple visits to the same shop. I use relation config, and a table is displayed, having an "Add" button on top. The problem is that when I press the "Add" button, the list of shops appears, but that won't contain any shop, which has already been added to the pivot table for the current user.

How could I resolve this in the simplest way?


Solution

  • In the RelationController it's being prevented to add multiple times the same link in the pivot table (https://github.com/wintercms/winter/blob/develop/modules/backend/behaviors/RelationController.php, lines 936-945). One solution would be to extend the RelationController, to override that method and to delete these lines.

    I used a different approach, where I transformed the pivot table to a regular table. Now there are one-to-many relationships between visit-user and visit-shop. This way it's possible to add the same relationship multiple times.