search
HomeBackend DevelopmentPHP Tutorial两个表是多对多的关系,一定需要关联表吗?

有两张表规则表和组表:tb_rulestb_group

这是auth权限中的两张表,明显他们是多对多的关系,一个规则可以属于多个组,一个组可以拥有多个规则;

按道理说应该有一张关联表tb_group_rules(group_id,rule_id)

但是thinkPHPauth类里面不用关联表,是这样的tb_group里面有一个rules字段,保存'1,2,3,4,5,6,7,8,9'这样的规则id

我猜想他不用关联表可能有这几个考虑:
1:一般每个组的规则并不多,用一个字段比较管理方便,不必要建一个关联表造成资源的浪费
2:一般都是取得组的规则id,授权时直接更改组的规则字段

但是我觉得这样时候总不好:
1:什么叫一般组的规则不多,什么叫一般,这个谁说的准呢,反正我就是不放心
2:将规则用,分割好吗,如果要是想取消某一个规则,这样极为不方便(程序只能查出规则字段用,分割再取出,在拼接,在保存),给维护带来麻烦
3:通过组来更改规则是方便,但如果我想根据规则看它属于那些组时,或者根据规则为它授权某些组时就比较麻烦了

如果使用关联表,上面都不是问题,也利于程序的维护

上面就是我的理解和利弊的分析,如有不对请大神指正

我探讨的问题,如题,到底什么时候使用关联表,以及利弊的权衡,比如此案列中的情况。

回复内容:

有两张表规则表和组表:tb_rulestb_group

这是auth权限中的两张表,明显他们是多对多的关系,一个规则可以属于多个组,一个组可以拥有多个规则;

按道理说应该有一张关联表tb_group_rules(group_id,rule_id)

但是thinkPHPauth类里面不用关联表,是这样的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在设计的时候并未直接考虑这点。

Statement
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.