两个表是多对多的关系,一定需要关联表吗?
有两张表规则表和组表:tb_rules
,tb_group
这是
auth
权限中的两张表,明显他们是多对多的关系,一个规则可以属于多个组,一个组可以拥有多个规则;
按道理说应该有一张关联表tb_group_rules(group_id,rule_id)
但是thinkPHP
的auth
类里面不用关联表,是这样的tb_group
里面有一个rules
字段,保存'1,2,3,4,5,6,7,8,9'
这样的规则id
;
我猜想他不用关联表可能有这几个考虑:
1:一般每个组的规则并不多,用一个字段比较管理方便,不必要建一个关联表造成资源的浪费
2:一般都是取得组的规则id
,授权时直接更改组的规则字段
但是我觉得这样时候总不好:
1:什么叫一般组的规则不多,什么叫一般,这个谁说的准呢,反正我就是不放心
2:将规则用,分割好吗,如果要是想取消某一个规则,这样极为不方便(程序只能查出规则字段用,分割再取出,在拼接,在保存),给维护带来麻烦
3:通过组来更改规则是方便,但如果我想根据规则看它属于那些组时,或者根据规则为它授权某些组时就比较麻烦了
如果使用关联表,上面都不是问题,也利于程序的维护
上面就是我的理解和利弊的分析,如有不对请大神指正
我探讨的问题,如题,到底什么时候使用关联表,以及利弊的权衡,比如此案列中的情况。
回复内容:
有两张表规则表和组表:tb_rules
,tb_group
这是
auth
权限中的两张表,明显他们是多对多的关系,一个规则可以属于多个组,一个组可以拥有多个规则;
按道理说应该有一张关联表tb_group_rules(group_id,rule_id)
但是thinkPHP
的auth
类里面不用关联表,是这样的tb_group
里面有一个rules
字段,保存'1,2,3,4,5,6,7,8,9'
这样的规则id
;
我猜想他不用关联表可能有这几个考虑:
1:一般每个组的规则并不多,用一个字段比较管理方便,不必要建一个关联表造成资源的浪费
2:一般都是取得组的规则id
,授权时直接更改组的规则字段
但是我觉得这样时候总不好:
1:什么叫一般组的规则不多,什么叫一般,这个谁说的准呢,反正我就是不放心
2:将规则用,分割好吗,如果要是想取消某一个规则,这样极为不方便(程序只能查出规则字段用,分割再取出,在拼接,在保存),给维护带来麻烦
3:通过组来更改规则是方便,但如果我想根据规则看它属于那些组时,或者根据规则为它授权某些组时就比较麻烦了
如果使用关联表,上面都不是问题,也利于程序的维护
上面就是我的理解和利弊的分析,如有不对请大神指正
我探讨的问题,如题,到底什么时候使用关联表,以及利弊的权衡,比如此案列中的情况。
对应你觉得这样时候总不好的三点说明,我进行回复个人观点,(因为您的问题,公说公有理婆说婆有理)。
1、一般,你自己猜想了一般情况,而又是你自己否定这个一般情况,那么这是你自己的问题了。
2、分割,如果rules字段现在存储的是‘1,2,3,4,5,6,7,8,9’,那么在管理界面进行删除2号角色的时候,不是对现有存储的rules字段值进行分割,剔除2,在组合。而是在管理界面删除后点击保存,表单重新提交组所拥有的角色信息进行组合拼接,然后update保存rules字段信息。而并不是你所描述的情况:“分割再取出,在拼接,在保存”,这的确给维护带来麻烦,但是实际做法不该是描述的这样子。
3、“但如果我想根据规则看它属于那些组时”,这应该是你业务需要,thinkPHP在设计的时候并未直接考虑这点。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.
