I have two tables that are set up, one named users and one named work_order.
In the users table, the fields include *id, given_name, family_name*
while the work_order table includes *id, create_date, due_date, description*
I’d like to add the created_by, requested_by, and owned_by fields, which would all be limited to the users listed in the users table. What is the correct way to do this? Would I have the “_by” fields in the work order table or would I need to have a new table with the work_order_id, user_id, and a role field of some sort?
First of all, your table should be ‘work_orders’ (plural), not ‘work_order’ (singular). Not a big deal, as long as you specify
$useTable = "word_order";in your model, but – naming convention says tables should be plural.Because you’re just dealing with three things, and don’t expect to continuously add new fields, you’re completely fine (and probably what I’d do) using the three fields you mentioned: ‘created_by’, ‘requested_by’, and ‘owned_by’ fields in your work_orders table.
I suppose if it were me, I’d follow the
something_idconvention and use ‘creator_id’, ‘requester_id’, and ‘owner_id’ though. Either way, it’s up to you.Then, when you set up your associations, you’ll just use those three fields to associate with the ‘User’ model. Off the top of my head, it would be something like: