KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

How to correctly model a resource with two primary key attributes #2473

Open Wayland Games opened this topic on on Dec 1, 2021 - 1 comments

Wayland Games commented on Dec 1, 2021

We have a resolving table of UserRole which has a primary key made up of id_user AND id_role.

How would this be correctly modelled as a resource?

I've tried BelongsTo relations but these don't seem restrictive of the update. When deleting a record it deletes all of the records for one half of the key ie its executing delete from userrole where id_user = 1 rather than delete from userrole where id_user = 1 AND id_role = 1

KoolReport commented on Dec 2, 2021

I suggest that you don't model the UserRole table. This table is just the bridge between two true entities which are User and Role. Instead you can use the HasMany relations to link between User and Role. If you want to add/delete Role for an User, you create Action to handle it. For instance, you create a ChangeRoleActions which has a form. Inside a form, you have an Select2 and list all the available roles (as well as selected roles for that user). You can add/remove list of roles there. After selected, in the handle() method of action, you will get list of should-have roles. You make query to delete all current roles of that users and then immediately add list of show-have roles. Not sure if my explanation makes sense.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Dashboard