We are currently considering migrating our database code in php to Doctrine2. It seems to us that most of the things we would like the do are possible with Doctrine2. There is only one thing which we are not sure if it is possible and how we should do that. Our database model can’t be changed right now because another project is using the same database. We can only change one thing at a time. Of course we are also curious how to improve the database model but that is not our main concern.
The issues is like this. We have a product table containing basic product information like this:
[product_id | code | name | description | price]
4 AS84GD Steel brush Strong steel brush..... 25.50
28 HDD21N Sand paper Sand paper for wood..... 0.40
etc
We have different users for our product which al have different extra product properties. So we have an product_extra_field table which could look like this:
[extra_field_id | name]
3 weight
4 color
9 supplier
and finally the table with the extra data product_extra_field_data
[product_id | extra_field_id | value]
4 3 280
4 4 brown
4 9 company_x
28 3 3
28 4 yellow
28 9 company_y
How do we right the necessary Doctrine2 code to get this data in one object, so we could have an object like this?
[Product]
Id : 4
Code : AS84GD
Name : Steel brush
Description : Strong steel brush.....
Price : 25.50
Weight : 280
Color : brown
Supplier : company_x
I’ve added a tag for Hibernate because I can read (N)Hibernate code and translate the ideas to Doctrine2.
So far, I don’t think you can achieve that with mapped superclasses.
But, you could use native SQL within the doctrine result set mapping:
http://docs.doctrine-project.org/en/latest/reference/native-sql.html
Those are one of the drawbacks when mapping between objects and relational data, I mean, if you really want just one entity instead of replicating the relationships from the database.