两个表是多对多的关系,一定需要关联表吗?
有两张表规则表和组表: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

AI Hentai Generator
Generate AI Hentai for free.

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...
