Well I know inheritance mapper is used in data mapper pattern to overcome the lack of inheritance issue for SQL database tables. Martin Fowler wrote 25+ pages describing the three common inheritance mapping patterns: Single-Table inheritance, Class-Table inheritance and Concrete-Table inheritance. Doctrine also utilizes inheritance mapping to some extent, its used in some projects. I’m trying to work on my own inheritance mapper for the user hierarchy(user - member - admin/mod/banned) and permission hierarchy (permission - modpermission/adminpermission) , but then I realize there is a big problem with inheritance mapping.
In Martin Fowler’s book, he uses some kind of type code to determine the correct domain model in the inheritance hierarchy, but the real issue is, where is this type code coming from? When we are fetching data from the database, we usually are only given the ID of its corresponding domain object. How is inheritance mapper supposed to know what is the correct object to create just given an ID, especially when the ID is in integer format and contains no additional information?
One way I can think of is that the type code for each record is stored somewhere in the database, so you have to look for the typecode given the object ID and then use the typecode to create correct domain model. But this results in at least 2x more DB access. Another possible solution is to use string ID prefixed by abbreviation of table name(or domain model’s type), this way the mapper can scan and identify the typecode by reading the ID. There are problems with this approach too, string IDs are not as optimized as integer IDs in relational database and it may not be faster than the last method after all. Also note that Martin fowler frequently used type casting in his book to convert domain objects to their appropriate types, this is not possible here either since PHP is not Java.
I am definitely out of idea here, what do you think about this problem? And how can it be resolved?