Answer the question
In order to leave comments, you need to log in
How to implement the possibility of registering different types of users?
What is the best way to implement the ability to register different users (individual or legal) so that each type has different data fields (for example, a legal entity does not have fields, last name, first name, etc.), but one entity, i.e. "user".
Answer the question
In order to leave comments, you need to log in
Give the user a role (Individual / Legal entity)
Add all role-specific fields to a hash. If you use postgres - hstore, if sqlite or mysql - then store accessor with a text type field.
If you do not have postgres and you need to index this information, then you are out of luck.
The first easiest option is STI . But you need to understand that if there are many different fields, then there will be many zero values in the database. This is not so very critical, but it is necessary to remember and understand.
If the first option does not fit (you do not use postgres or mongo, that is, there is no NoSQL support), then you need to normalize the database.
Make a user. And the user has a connection with the model PhysicalUserInfo, LegalEntityUserInfo
Or, as a variant of normalization, you create a "Property" entity that belongs to the role. And one user will have one number of properties (for individuals), the other - another (for legal entities) (then, of course, also put the role into the model for order)
martinfowler.com/eaaCatalog/singleTableInheritance.html
martinfowler.com/eaaCatalog/classTableInheritance.html
martinfowler.com/eaaCatalog/concreteTableInheritan...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question