mysql - In Thinkphp's RBAC authentication, why does the 'user-role' table need an independent table? ?

WBOY
Release: 2016-08-18 09:15:46
Original
851 people have browsed it

You can refer to the five tables here: http://www.lyblog.net/detail/...

Actually there is a table:
mysql - In Thinkphp's RBAC authentication, why does the 'user-role' table need an independent table? ?

There is a one-to-one correspondence between users and roles. Why not just add a role field to the user table instead of creating another table?
What are the benefits of doing this?

Supplement:
Some people say that users and roles have a one-to-many relationship. I want to know why it is necessary to set it to one-to-many?
Isn’t it easier to handle a one-to-one relationship?

Reply content:

You can refer to the five tables here: http://www.lyblog.net/detail/...

Actually there is a table:
mysql - In Thinkphp's RBAC authentication, why does the 'user-role' table need an independent table? ?

There is a one-to-one correspondence between users and roles. Why not just add a role field to the user table instead of creating another table?
What are the benefits of doing this?

Supplement:
Some people say that users and roles have a one-to-many relationship. I want to know why it is necessary to set it to one-to-many?
Isn’t it easier to handle a one-to-one relationship?

My understanding is that a user can have multiple roles

It’s like this:

A person can have multiple roles
A role can also be used by multiple people
Many-to-many relationships generally require an intermediate table to be mapped.

Unless you are using a non-relational database like redis

Not one-to-many, but many-to-many.

Maybe your project doesn’t need it, but as a framework, you need to consider broader needs. From this perspective, a many-to-many table can be used as a one-to-one table, because many-to-many includes one-to-one, but one-to-one A one-to-one design will inevitably fail to meet the many-to-many needs. So there's going to be a separate relationship table here, got it?

A user has multiple roles.

In fact, it is possible not to create a separate table.

Decoupling, single responsibility model

The relationship of ManytoMany requires an intermediate table!

In order to meet the many-to-many needs
My understanding may not be accurate: For example, there are several roles a, b, and c. According to your understanding, users 1, 2, and 3 can correspond to a, b, and c respectively. The character is okay. If another user 4 comes, the role he needs happens to have the permissions of roles a and b, so user 4 directly belongs to roles a and b. There is no need to create a new role d to meet the needs of user 4

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!