5

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

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
MrMuh
  • 319
  • 1
  • 4
  • 13

2 Answers2

17

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

TlmaK0
  • 3,578
  • 2
  • 31
  • 51
  • 2
    @MrMuh This is correct and helpful, while the accepted answer is incorrect/outdated. Please consider making this the accepted answer. – Sean the Bean Nov 04 '15 at 21:51
  • 1
    Correct link [Doctrine. ORM. Documentation. Inheritance Mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/inheritance-mapping.html) – Maxim Mandrik Dec 14 '18 at 16:24
-6

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.

adlawson
  • 6,303
  • 1
  • 35
  • 46
  • 1
    +1. Also, it is not a good idea to have a class that encapsulates data from several database tables. You'd better merge tables into single one, or design two classes. In your current approach it is some kind of [`Divergent Change`](http://sourcemaking.com/refactoring/divergent-change). So, sooner or later you'll have to refactor it. – J0HN Aug 26 '11 at 07:19
  • 3
    -1 3rd normal form actually sais that you can separate the data based on context. You could have 2 tables users and user details . But accordingly to 3rd normal form user details must be split into adress details (street, city, country), personal details (gender, height, age, income, no_children), contact_details (phone_no, PoBox) and so on. Doctrine is still a weak ORM , is far behind the ones that other mature languages have implemented . As example even today it doesnt have full suport for DDD value objects (or collections of VO) . Your answer was in '11 :) – Tudor May 05 '14 at 23:43
  • 2
    "This is not possible. It also doesn't make sense to do so." This is incorrect. (Perhaps it was true in Doctrine 1?) The link in @tlmak0 's answer clearly demonstrates that it's possible. And the OP laid out a scenario in which I think it makes perfect sense to keep the two tables separate because they store logically different sets of information (perhaps there'd be a case where you want to assign a user to a different WP account?), and it would be wise not to merge them in the interest of preserving the WordPress DB structure. – Sean the Bean Nov 04 '15 at 21:56