Database design multiple user types CakePHP
Due to CakePHP limitation to only use User as the road to go in Auth, I have to change my user type setup. I have tried some fancy code to overwrite the identify() method in the Auth component but that did not solve the problem. I use a Role field to distinguish between premium and standard users.
I have Users and Companies that need to login and perform actions separately. Why not 1 user table with a usertype field? Because they have 75% different fields inside the table.
In my new setup, I have 1 User table and a HasOne relationship with a Company or a Member (former User table). Inside the User table, the common fields reside including the email address and password for login (to tackle the Auth force to use User).
It looks like this:
Table users:
- id
- name
- password
- member_id, NULL (FK)
- company_id, NUll (FK)
- role_id (FK)
Table member
- id
- specific field x
- specific field y
- ...
Table companies
- id 开发者_JAVA百科
- specific field a
- specific field b
- ...
The ugly thing is that the User table will always have a Null, as a user is a Member or a Company
ANOTHER solution could be to point the FK to the other side. Hereby, the user_id will be the foreign key in the Member and Companies table.
Table users:
- id
- name
- password
- role_id (FK)
Table member
- user_id (FK / PK)
- specific field x
- specific field y
- ...
Table companies
- user_id (FK / PK)
- specific field a
- specific field b
- ...
I am stuck and don't have the normalization & database design experience to choose between the options. Or maybe there both solutions are terrible and another solution should be it. Thanks!
well you should use the groups table to handle user types, i.e. one user table with common fields like username and password and a foreign key belonging to the groups table, and one table for groups groups table could have different types of group like normal_user or company. This way you will be able to handle as many different user types as you want. And even if you have many different fields for each type of user you can make separate tables for them (If you have only 2-3 user types witch are absolutely different) like company_details user_details and add fields specific to that type of user in them, and include one foreign key along with a type field in groups table. Now groups table will have details_id from either details table. You can select the specific data based on the type field in groups i.e. if the type is normal_user join with user_detail table else if it is company join with company table. But you will have to do some manual work because cake wouldn't be able to join groups table properly.
Table: groups id, details_id(Not unique), type, name, other_fields...
Table: users id, group_id, username, password, other_fields...
Table company_detail id, specific fields... Other Groups tables
精彩评论