Page 1 of 1
How to Structure Model for Class Table Inheritance?
Posted: Fri Jan 16, 2009 2:43 pm
by gabriel1836
I have several instances that represent Class Table Inheritance with a base table like Person and then tables that contain type-specific information like User, Prospect, etc. My initial thought (since I'm using Table Data and Row Data Gateway patterns in Zend_Framework) was to return a separate row gateway for each of these tables and the Model layer would handle each row manually within an array.
Kinda like so:
Code: Select all
/**
Contains instances of Zend_Db_Table_Row or a subclass of Zend_Db_Table_Row_Abstract
**/
protected $_data = array(
'person',
'user',
'prospect'
);
However, I have now realized that this was probably a foolish decision as it couples the database structure to the Model layer so now I'm wondering if I need to return a composite of all the appropriate properties from a single query to the model and then handle what properties belong to which table inside the gateways. This has the benefit of completely abstracting the database structure from the model but it will add some complexity to the scenario because I was currently using a Factory method to determine and instantiate the appropriate type of Person. Now I would have to use the Factory method within the model as well as test on type in the data access layer to determine what fields to return to the model.
Does anyone have suggestions on how to do this better or does this sound like a valid way of handling the situation?
Re: How to Structure Model for Class Table Inheritance?
Posted: Fri Jan 16, 2009 5:36 pm
by fredrik
Class Table inheritance is imho. a no-go in PHP due to how complex it is, which makes it inherently expensive to both query and compute the result. You will be much better of using Single Table Inheritance (PoEAA naming) with NULL:able fields.
I just don't even want to imagine the headache of trying to shard a database schema that supports class table inheritance

Re: How to Structure Model for Class Table Inheritance?
Posted: Sun Jan 18, 2009 9:58 am
by gabriel1836
I acknowledge the extra expense of having to perform at least two queries: One to determine type, and a second to get the data from the proper fields. It does complicate the object-relational-mapping but I'm confused as to why no one appears to have written or done anything with this idea in PHP. It doesn't seem like it should be that impossible.
Re: How to Structure Model for Class Table Inheritance?
Posted: Mon Jan 19, 2009 8:52 am
by allspiritseve
gabriel1836 wrote:I have several instances that represent Class Table Inheritance with a base table like Person and then tables that contain type-specific information like User, Prospect, etc.
What is the difference between Person, User, and Prospect? Person and user seem to be the same thing to me. Prospect seems like it would differ by a boolean: 1= prospect or 2 = client. I guess I don't see the need to class table inheritance unless you have an existing deep inheritance hierarchy that somebody else wrote and you're not able to rewrite them. Otherwise, I'd say stick to one table per class.
gabriel1836 wrote:Does anyone have suggestions on how to do this better or does this sound like a valid way of handling the situation?
My suggest would be to make a Gateway for each unique domain object. The gateway is responsible for getting the data for that single object, whether that means querying one table or ten. The gateway is also responsible for persisting that object.
gabriel1836 wrote:I acknowledge the extra expense of having to perform at least two queries: One to determine type, and a second to get the data from the proper fields. It does complicate the object-relational-mapping but I'm confused as to why no one appears to have written or done anything with this idea in PHP. It doesn't seem like it should be that impossible.
It's not impossible, it's just a lot of work. Personally, I rarely use inheritance anyways. Composition is much more flexible and easier to test.
Re: How to Structure Model for Class Table Inheritance?
Posted: Mon Jan 19, 2009 11:29 am
by gabriel1836
For clarification:
Person serves as the base table which contains information common to all of the inheriting classes.
A User is someone with login credentials such as a username and password and stored information such last IP address used and last log in date.
Supervisors and Buyers are extended Users with additional access within the system and additional table from which they pull relevant data.
A Prospect is not a user and maps to certain very specific data that is not relevant for any other users.
Single Table Inheritance I don't think will really work because it would bloat the core table needlessly and I'd end up with a single table that contained many extra fields like 50. Additionally some of the data which the Buyer maps to cannot be refactored into the core person table so at some level I need to do some complex modeling no matter.
After doing a lot of reading of different approaches myself it seems like my best bet will be to write a data mapper to handle this scenario.
Re: How to Structure Model for Class Table Inheritance?
Posted: Mon Jan 19, 2009 11:41 pm
by josh
Create the tables, write tests, write code, refactor. Are you trying to create a data mapper or what?
Re: How to Structure Model for Class Table Inheritance?
Posted: Tue Jan 20, 2009 1:43 am
by matthijs
Would it not make more sense to have one table for users and then another table for Roles (or something like that)? And then whatever other tables you need for additional information.
That way you don't need "inheritance". The Roles table just holds a foreign key to the Users table. And additional information in other tables again can be related to the Roles with foreign keys.
If I were you I would start with the database design. Make sure you have a good level of normalization. Then only after that start thinking about how your code is going to get the data out of the database. That way, you are sure that the database design is correct and flexible enough.
Re: How to Structure Model for Class Table Inheritance?
Posted: Tue Jan 20, 2009 3:16 am
by Jenk
jshpro2 wrote:Create the tables, write tests, write code, refactor.
glad I'm not the only one who thought that.
