应用程序和数据库之间的单一数据键?

发布于 2024-08-12 11:01:50 字数 963 浏览 8 评论 0原文

是否有一种范例,我可以在一个地方且仅在一个地方更改数据键名称,并让应用程序和数据库正确处理它?

我最近使用类常量来映射到数据库字段名称,但是 我仍然必须使它们与原始数据库键保持一致。

我的意思是,以 PHP 为例,现在我可能会使用

$infoToUpdateUser[ User::FIELD_FIRST_NAME ]

这意味着当我在常量处更改​​它时,我不必搜索更改对该字段的所有引用的代码。

另一个出现这种情况的领域是引用字段。由于一些早期糟糕的设计决策,我有,例如,这些类型的表:(

表名:primary_key)

  • cats:cat_id
  • dogs:dog_id
  • parrots:bird_id(记住,糟糕的设计,因此parrots/bird_id之间不匹配)
  • lizards: lizard_id

然后假设我有一系列更新记录的表单类。

  • AnimalForm
  • DogForm 扩展 AnimalForm
  • CatForm 扩展 AnimalForm
  • ParrotForm 扩展 AnimalForm

现在我想使用父类 AnimalForm 中的更新函数来更新 SQL 数据库中的记录,因此我不必在 20 个子类中复制代码。

但是我不知道如何概括更新查询,因此当前每个子类都有一个 idFieldName 成员变量,并且父类将其插入到查询中,例如

"UPDATE " 。 $这个->表。 “设置<数据>位置”。 $this->idFieldName

这样做似乎很草率,但目前我想不出更好的解决方案。

是否有一种设计模型或范例可以链接在一起或抽象数据键名称以供数据库和应用程序共享作为参考?

Is there a paradigm in which I can change a data-key name in one place and one place only, and have it properly be dealt with by both the application and database?

I have resorted most recently to using class constants to map to database field names, but
I still have to keep those aligned with the raw database keys.

What I mean is, using PHP as an example, right now I might use

$infoToUpdateUser[ User::FIELD_FIRST_NAME ]

This means that when I change it at the constant, I don't have to search through the code to change all references to that field.

Another area this crops up in is in referencing fields. Due to some early poor design decisions, I have, for example, these sorts of tables:

( table name : primary_key )

  • cats : cat_id
  • dogs : dog_id
  • parrots : bird_id (remember, poor design, thus the mismatch between parrots / bird_id)
  • lizards: lizard_id
  • etc

Then let's say I have a series of form classes that update records.

  • AnimalForm
  • DogForm extends AnimalForm
  • CatForm extends AnimalForm
  • ParrotForm extends AnimalForm
  • etc

Now I want to update a record in the SQL database using an update function in the parent class, AnimalForm, so I don't have to replicate code in 20 subclasses.

However I do not know of a way to generalize the update query, so currently each subclass has an idFieldName member variable, and the parent class inserts that into the query, like

"UPDATE " . $this->table . " SET <data> WHERE " . $this->idFieldName

It seems sloppy to do it this way but I can't think of a better solution at this point.

Is there a design model or paradigm that links together or abstracts data-key names to be shared as a reference by both a database and an application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-08-19 11:01:50

您正在寻找的称为对象关系映射层。

ORM 通过将关系数据库映射到对象模型,将数据访问的关注点与业务逻辑分开。由于 ORM 会完成所有转换,因此如果您更改数据库表或列的名称,您只需告诉 ORM 一次,它就会将该更改正确应用到您的所有代码。

由于您表明您正在使用 PHP,因此这里有一个解决 PHP 中的 ORM 库的问题。有关 ORM 技术的其他信息可以在 Wikipedia 中找到

What you are looking for is called an Object-Relational Mapping layer.

An ORM separates the concerns of data access from business logic by mapping a relational database into an object model. Since the ORM does all the translation, if you change the name of a database table or column, you only have to tell the ORM once, and it will properly apply that change to all of your code.

Since you indicate that you are using PHP, here is a question that addresses ORM libraries in PHP. Additional information about ORM technologies can be found in Wikipedia.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文