Doctrine 2.1 - Map entity to multiple tables

无人久伴 提交于 2019-12-17 16:58:19

问题


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)?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/7198271/doctrine-2-1-map-entity-to-multiple-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!