在关系数据库中存储静态(类)属性

时间:2021-05-28 16:56:18

Suppose you store instances of a class in a relational table. How would you go about persisting a static attribute of that class? For example:

假设您在关系表中存储类的实例。你会如何坚持该类的静态属性?例如:

class WebSiteUser {
    private static $common_homepage_content;
    private $username;
    private $password_hash;
    ...
}

Corresponding to:

CREATE TABLE web_site_users
(
  username character varying(100) NOT NULL,
  password_hash character varying(40) NOT NULL,
  ...

Where does $common_homepage_content go then?

那么$ common_homepage_content去哪儿了?

2 个解决方案

#1


Well, if the variable you want to persist is just static I must assume that there are going to be many users (WebSiteUser) with the same $common_homepage_content. In this case, you should create a new table in the db just for this attribute because it's a 1->N relation.

好吧,如果你想要持久化的变量只是静态的,我必须假设有许多用户(WebSiteUser)具有相同的$ common_homepage_content。在这种情况下,您应该在db中为此属性创建一个新表,因为它是1-> N关系。

#2


As static variables make sense for a class but not on the instance level it can't go inside the table that has instance variables. In the table that you are creating there can be multiple username characters and corresponding password_hashes but putting common_homepage_content in each record would be duplication of data. You can put common_homepage_content in a separate table and have a reference to it from each of your records in the first table.

由于静态变量对于类有意义,但在实例级别上没有意义,因此它不能进入​​具有实例变量的表。在您正在创建的表中,可以有多个用户名字符和相应的password_hashes,但在每个记录中放置common_homepage_content将是重复数据。您可以将common_homepage_content放在单独的表中,并从第一个表中的每个记录中引用它。

#1


Well, if the variable you want to persist is just static I must assume that there are going to be many users (WebSiteUser) with the same $common_homepage_content. In this case, you should create a new table in the db just for this attribute because it's a 1->N relation.

好吧,如果你想要持久化的变量只是静态的,我必须假设有许多用户(WebSiteUser)具有相同的$ common_homepage_content。在这种情况下,您应该在db中为此属性创建一个新表,因为它是1-> N关系。

#2


As static variables make sense for a class but not on the instance level it can't go inside the table that has instance variables. In the table that you are creating there can be multiple username characters and corresponding password_hashes but putting common_homepage_content in each record would be duplication of data. You can put common_homepage_content in a separate table and have a reference to it from each of your records in the first table.

由于静态变量对于类有意义,但在实例级别上没有意义,因此它不能进入​​具有实例变量的表。在您正在创建的表中,可以有多个用户名字符和相应的password_hashes,但在每个记录中放置common_homepage_content将是重复数据。您可以将common_homepage_content放在单独的表中,并从第一个表中的每个记录中引用它。