原则 2.1 - 将实体映射到多个表

发布于 2024-12-01 06:15:52 字数 413 浏览 6 评论 0原文

我有以下数据库情况:

wp_users (user table generated by wordpress)
ID | user_login | ... 

wp_sp_user (extension to the wp_users table)
ID (FK) | surname | address | ... 

现在我已经尝试了几个小时将这两个表“融合”到一个 User 实体中,例如:

class User {
  var ID;
  var user_login;
  var surname;
  var address;
  ...
}

有什么方法可以在不修改wp_user 表(由于更新原因我不想这样做)?

I have the following database situation:

wp_users (user table generated by wordpress)
ID | user_login | ... 

wp_sp_user (extension to the wp_users table)
ID (FK) | surname | address | ... 

Now I've already been trying for hours to "fuse" those two tables into one single User entity, e.g:

class User {
  var ID;
  var user_login;
  var surname;
  var address;
  ...
}

Is there any way to accomplish such a mapping without modifying the wp_user table (which I don't want to do for updating reasons)?

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

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

发布评论

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

评论(2

三寸金莲 2024-12-08 06:15:52

有时数据库重构是不可能的,或者表有自己的“存在理由”。在这种情况下,您可以使用继承。您的 User 类可以扩展 Account。将帐户映射到 wp_users 并使用 wp_sp_user 表对其进行扩展。用户类将使用两个表的列。

以下是学说文档:

https: //www.doctrine-project.org/projects/doctrine-orm/en/current/reference/inheritance-mapping.html

Some times database refactoring is not possible or the table has his own "raison d'être". In this cases you can use inheritance. Your User class can extens Account. Map Account to wp_users and extend it with wp_sp_user table. User class will use columns of the two tables.

Here is the doctrine documentation:

https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/inheritance-mapping.html

少跟Wǒ拽 2024-12-08 06:15:52

这是不可能的。这样做也是没有意义的。

您需要在 MySQL 中将表物理合并在一起,并为该表创建一个 Doctrine 实体。这是确保数据干净且完全规范化的唯一方法。

另一种可能的解决方案是为每个表创建一个实体,并使用业务对象来组合每个表的结果。这根本不是一个很好的解决方案,因为您必须处理应用程序层上的约束,并且您启动的查询量将增加一倍。

This is not possible. It also doesn't make sense to do so.

You will need to physically merge the tables together in MySQL and create a Doctrine entity for that table. This is the only way you can ensure your data is clean and fully normalized.

Another possible solution is to create one entity for each table and use a business object to combine results from each. This is not a very nice solution at all, as you will have to handle constraints on the application layer, and you will double the amount of queries you launch.

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