简体   繁体   English

从mysql表中的php存储类别名称的最佳方法

[英]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: 这是我考虑的解决方案:

Second table with a one-to many relation 具有一对多关系的第二张桌子

This is the solution I'm currently using. 这是我目前正在使用的解决方案。

The database would look something like this: 该数据库将如下所示:

items 项目

  • INT id (primary) INT ID(主要)
  • INT category_id (index) INT category_id(索引)

categories 分类

  • INT id (primary) INT ID(主要)
  • VARCHAR name (unique) VARCHAR名称(唯一)

Downsides (for me): 缺点(对我而言):

  • I have to store the categories in the database. 我必须将类别存储在数据库中。 For the moment I have a sort of seeding that adds all my categories to the database. 目前,我有种选择,可以将所有类别添加到数据库中。 But that way the data is in at least two places (DRY !), and I have to update the seeding script each time I do a modification 但是这样一来,数据至少存在两个位置(DRY!),并且每次修改时我都必须更新种子脚本。
  • When I do a query, I have to use two tables, because the php code only knows the "name" 当我执行查询时,我必须使用两个表,因为php代码仅知道“名称”
  • When I insert a record I have to do a subrequest to find the category id first from the "name" 当我插入记录时,我必须先执行一个子请求才能从“名称”中找到类别ID

Mysql enums MySQL枚举

The database would look something like this: 该数据库将如下所示:

items 项目

  • INT id (primary) INT ID(主要)
  • ENUM category (index) [hat, shirt, socks, ...] 枚举类别(索引)[帽子,衬衫,袜子...]

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. 它具有删除第一个解决方案的两表查询的优点,但是数据仍然是重复的,并且比以往任何时候都更难以对数据库进行存储。

String field in the items table 项目表中的字符串字段

The database would look something like this: 该数据库将如下所示:

items 项目

  • INT id (primary) INT ID(主要)
  • VARCHAR category (index) VARCHAR类别(索引)

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) 从查询角度来看,我的印象是索引的行为与我先前使用的第二个表相同(具有仅查询一个表和一个索引而不是两个表和两个索引的优点)

So, Problem... 所以,问题...

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.

相关问题 通过PHP查询从mysql表打印值的最佳方法 - Best way to print values from mysql table by querying through PHP 使用PHP从MySQL检索表的最佳方法是什么? - What is the best way to retrieve table from MySQL using PHP? 从mysql / php同时获取Category-info和Article-info的最佳方法是什么? - What is the best way to get Category-info and Article-info at same time from mysql/php? 从MySQL-> PostGres迁移:在MySQL中存储PHP对象,在PostGres中检索的最佳方法? - Moving from MySQL->PostGres: Best Way to Store PHP Objects in MySQL, Retrieve in PostGres? 在php中存储来自URL的图像的最佳方法? - Best way to store an image from a url in php? php mysql-我应该将“类别名称”字段添加到表中吗? - php mysql - should i add the field “category-name” to a table or not? mysql php按类别名称分组 - mysql php grouping by a category name 存储与 mysql 表的列相关的值的最佳方法? - Best way to store values related to columns of a mysql table? MySQL PHP从表B中获取与表A中给定列不匹配的条目的最佳方法是什么 - mysql php what is the best way to obtain from table B the entries that don't match a given column in table A 在PHP和MySQL中存储和搜索关键字以获取记录的最佳方法? - Best way to store and search keywords for a record in PHP and MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM