E
E
Evgeny Bukharev2016-04-25 10:36:55
MySQL
Evgeny Bukharev, 2016-04-25 10:36:55

What is the best way to store users in a database?

The situation is this:
There is a table with users, there is a table with companies. A company can have many users, a user can have one company. Relationship by the company_id field in the users table.
The whole system is designed to work with just such a structure: user->company
But there was a need to introduce a new type of user: freelancers.
That is, these are the same users, but without the company.
What is the best way to organize storage and work with such a layout, if, logically, the freelancer does not have a company, but there are some fields, for example, "Requisites", which are linked to the company for ordinary users, and are linked to the user for the freelancer?
To carry out such fields in the separate table? or despite the logic to create a company for a freelancer, and keep them there?
UPD: If we consider the table of companies for a freelancer, then only the company name and legal address are redundant in relation to the freelancer, everything else applies to him

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mitaichik, 2016-04-25
@evgenybuckharev

Here, in fact, the usual inheritance: there is an abstract User class, CompanyUser and FreelanceUser are inherited from it, which have their own characteristics (CompanyUser has a connection with the company, FreelanceUser has details).
How can this be done in Yii: https://github.com/samdark/yii2-cookbook/blob/mast...
But in general there are a lot of ways to implement it: schema-less databases, different tables, EAV, and so on.
I would make 3 tables: user, company, freelance_requesite. In CompanyUser I would make a relay on company, in FreelanceUser - on details, and would use the mechanism from cookbook'a.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question