Php 是否存在表示不同表中多个对象的规范化形式?
对于我当前正在构建的系统,会出现以下两种情况:Php 是否存在表示不同表中多个对象的规范化形式?,php,mysql,design-patterns,architecture,normalization,Php,Mysql,Design Patterns,Architecture,Normalization,对于我当前正在构建的系统,会出现以下两种情况: My permissions system是将权限附加到角色、将角色附加到用户的常用系统,但有一个转折点:权限可以应用到任何层的任何资产,共有4个“层”,编号为0到3。因此,角色分配表有5个字段:资产层、资产ID、角色ID、用户ID和“禁用”标志。此“层/ID”系统还用于将用户分配到“主资产” 这个系统不幸的副作用是,在应用程序本身而不是在SQL中解析这些资产引用通常要简单得多(我知道这是可能的,但它会导致一些非常难看的查询)有没有更好的方法来表示
id int(5) unsigned not null primary key auto_increment,
name varchar(32) not null,
disabled tinyint(1) unsigned not null,
第2级:
id int(5) unsigned not null primary key auto_increment,
parentId int(5) unsigned not null,
name varchar(32) not null,
abbr varchar(16) not null,
disabled tinyint(1) unsigned not null,
foreign key (parentId) references tier1(id)
第3级:
id int(5) unsigned not null primary key auto_increment,
parentId int(5) unsigned not null,
name varchar(32) not null,
abbr varchar(16) not null,
disabled tinyint(1) unsigned not null,
foreign key (parentId) references tier2(id)
权限:
id int(5) unsigned not null primary key auto_increment,
permission_key varchar(16) not null,
permission_desc varchar(128) not null
role_id int(5) unsigned not null,
permission_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
primary key (role_id, permission_id),
foreign key (role_id) references roles(id),
foreign key (permission_id) references permissions(id)
tier_id tinyint(1) unsigned not null,
asset_id int(5) unsigned not null,
user_id int(5) unsigned not null,
role_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
primary key (tier_id, asset_id, user_id, role_id),
foreign key (user_id) references users(id),
foreign key (role_id) references roles(id)
角色:
id int(5) unsigned not null primary key auto_increment,
name varchar(32) not null,
tier1_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
foreign key (tier1_id) references tier1(id)
角色权限:
id int(5) unsigned not null primary key auto_increment,
permission_key varchar(16) not null,
permission_desc varchar(128) not null
role_id int(5) unsigned not null,
permission_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
primary key (role_id, permission_id),
foreign key (role_id) references roles(id),
foreign key (permission_id) references permissions(id)
tier_id tinyint(1) unsigned not null,
asset_id int(5) unsigned not null,
user_id int(5) unsigned not null,
role_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
primary key (tier_id, asset_id, user_id, role_id),
foreign key (user_id) references users(id),
foreign key (role_id) references roles(id)
用户\角色\权限:
id int(5) unsigned not null primary key auto_increment,
permission_key varchar(16) not null,
permission_desc varchar(128) not null
role_id int(5) unsigned not null,
permission_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
primary key (role_id, permission_id),
foreign key (role_id) references roles(id),
foreign key (permission_id) references permissions(id)
tier_id tinyint(1) unsigned not null,
asset_id int(5) unsigned not null,
user_id int(5) unsigned not null,
role_id int(5) unsigned not null,
disabled tinyint(1) unsigned not null,
primary key (tier_id, asset_id, user_id, role_id),
foreign key (user_id) references users(id),
foreign key (role_id) references roles(id)
您是否考虑过将权限表拆分为一组表,每个表应用于单独的对象表,例如tier_1_资产和tier_1_权限,tier_2_资产和tier_2_权限。这将使查询资产层的权限变得更简单,但查询用户的所有权限则更复杂(这是一种折衷) 我还想对关系数据库是数据的正确存储介质这一假设提出质疑。它可能确实是,但也可能不是。(PHP可能会限制这方面的自由) 如果您的数据集很小,为什么不将其保存到单个序列化文件中,并使用小型服务器将其保存在内存中并提供查询接口?我相信有充分的理由不这样做。(除了使用PHP之外) 您是否考虑过第二个问题的多点表格?它可能感觉是一种重复,但它确实使某些类型的查找更加简单,当将每个外键引用视为一种不同类型的对象时,被规范化(当表被分离时,您可以使用对它们的外键检查)。
对象之间关系的多样性应该告诉您将键和引用放在何处:一对多->(键(key);一对可选->(键)您可以用您的对象的可视表示更新您的问题吗