[英]Best way to store a category name from php in a mysql table
My project is a website written in php & js that use a mysql database. 我的项目是使用php和js编写的使用mysql数据库的网站。
I need to store an item with its "category" or "type" (couldn't find a better name for that). 我需要存储一个带有“类别”或“类型”的项目(找不到更好的名称)。 Each item can only have one category.
每个项目只能有一个类别。
Each category has a "name" (a sort of id) used by the php and javascript code, like "hat". 每个类别都有一个由php和javascript代码使用的“名称”(一种ID),例如“ hat”。
These "names" are hard-coded in the php/javascript code. 这些“名称”硬编码在php / javascript代码中。 They are not user-defined.
它们不是用户定义的。 There's a known list of them.
有一个已知的清单。 There can be new "names" with new versions of the application, and eventually some can be removed.
应用程序的新版本中可能会有新的“名称”,最终可以删除其中一些。
Here are the solutions I considered: 这是我考虑的解决方案:
This is the solution I'm currently using. 这是我目前正在使用的解决方案。
The database would look something like this: 该数据库将如下所示:
items 项目
categories 分类
Downsides (for me): 缺点(对我而言):
The database would look something like this: 该数据库将如下所示:
items 项目
It has the advantage to remove the two-tables query of the first solution, but the data is still duplicated, and more heavily bould to the database than ever. 它具有删除第一个解决方案的两表查询的优点,但是数据仍然是重复的,并且比以往任何时候都更难以对数据库进行存储。
The database would look something like this: 该数据库将如下所示:
items 项目
The category name is stored directly in the "items" table, with a index. 类别名称和索引直接存储在“项目”表中。 From the query view I have the impression that the index will act the same way that the second table I used previously (with the advantage of querying only one table and one index instead of two tables and two indexes)
从查询角度来看,我的印象是索引的行为与我先前使用的第二个表相同(具有仅查询一个表和一个索引而不是两个表和两个索引的优点)
What is the best way to do it, in term of performances and/or maintainability ? 就性能和/或可维护性而言,最佳方法是什么? Is there another better way to do it ?
还有另一种更好的方法吗?
Having integrity constraint would be good but not necessary. 具有完整性约束会很好,但不是必须的。 (I can manage this problem in another way)
(我可以通过其他方式解决此问题)
I have found the following thread: php mysql - should i add the field "category-name" to a table or not? 我发现了以下线程: php mysql-我是否应该将“类别名称”字段添加到表中? , but it was about a many-to-many relationship and looks like it was a variable list of categories.
,但关系是多对多的,看起来像是类别的可变列表。 So it doesn't really apply to my case.
因此,这实际上不适用于我的情况。
It depends, 这取决于,
How many categories will you have, how often will you need to update them? 您将拥有几个类别,需要多久更新一次?
I've taken the approach before where I added Enum, but also wrote a static method in a class and stored the valid names there 在添加Enum之前,我已经采用了这种方法,但是还在类中编写了一个静态方法并在其中存储了有效名称。
class Item{
public static function get_categories()
{
// only works in php >=5.4 otherwise use return array('Category1'...);
return [
'Category1' => 'Category1',
'Category2' => 'Category2',
'Category3' => 'Category3'
];
}
}
Doing the above you can easily get access to the array by Item::get_categories(), which should correspond to the values in the ENUM field in the database. 执行以上操作,您可以通过Item :: get_categories()轻松访问数组,该数组应对应于数据库中ENUM字段中的值。 If you need to add more categories, add them to the database and then to this function.
如果需要添加更多类别,请将其添加到数据库,然后添加到此功能。 You can use this to validate the users choice and generate the javascript options.
您可以使用它来验证用户的选择并生成javascript选项。
However!!!! 然而!!!!
You mention a seeding that populates your table, this wouldn't be able to easily update your ENUM for you. 您提到填充表的种子,这将无法轻松地为您更新ENUM。
I'd use an ENUM, because 我会使用一个ENUM,因为
1). 1)。 It's easy to setup 2).
易于设置2)。 It's slightly easier than joining the two tables (but JOINS are ace when you learn them) 3).
这比将两个表联接起来要容易一些(但JOINS是学习中的王牌)3)。 If you used a secondary table you'd (as you pointed out) have to lookup the value from that table first, and then insert, or if the category was used in a dropdown, use the category id and it's name to populate the dropdown (i'd do it this way)
如果您使用了辅助表,您(如您指出的那样)必须先从该表中查找值,然后插入,或者如果下拉列表中使用了类别,则使用类别ID及其名称来填充下拉列表(我会这样)
Final thought 最后的想法
The performance of both the ways (lookup table, and ENUM) you would barley even notice the difference unless your site is very very busy. 除非您的站点非常繁忙,否则两种方法(查找表和ENUM)的性能都可能会引起大麦的注意。 You'd still have to update the categories table with each version both ways, but it would be slightly easier to USE an INSERT statement instead of a ALTER TABLE
您仍然必须同时使用两种版本来更新类别表,但是使用INSERT语句代替ALTER TABLE会稍微容易一些
Whichever way you chose, if you don't like it, it would be quite easy to update your code. 无论您选择哪种方式,如果您不喜欢它,更新代码都将非常容易。
Hope this helps, and good luck 希望这会有所帮助,并祝你好运
Remember Keep It Simple Stupid KISS 记住保持简单愚蠢的吻
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.