Jump to content
Larry Ullman's Book Forums

Database For Objects With Inheritance

Recommended Posts

In one sentence: I've had a read around and if I'm not mistaken this issue is called "object relational impedance mismatch" and I would love to know a good solution to it within Yii.



I'm planning to build an application that goes beyond having the typical "user", "comment", "page", "file" database where you have comparatively straight forward models for each table (like in the book). One of the problems I am trying to solve looks something like this:




I have several levels of inheritance and I have no idea what the best way is to models that work with Yii. There is http://www.yiiframework.com/wiki/198/single-table-inheritance/ but that doesn't seem particularly clean especially once you have more than two tables (then again, I really can't judge). 

My head is spinning. If my question isn't clear please tell me what to expand on.




Share this post

Link to post
Share on other sites

Object relational impedance mismatch is a problem where your objects (or models) cannot be directly linked to the database structure. (And they should most often not) This is not an uncommon problem, as relational data most often be joined to create a model instance.


My question is whether you really need all this inheritance? The fact that you duplicate all these models really seem like a code smell to me. I don't know the problem you are trying to solve here, but it might be worth looking up a principle called "Composition over inheritence". That's fancy speak for "Interfaces instead of inheritance".


What that basically allows you to do is to remove a lot of inheritance while keeping classes to "contracts" they agree upon by implementing the interface. This will allow you to do

dependency injection instead of tighly binding your models together. This will also greatly simplify your UML scheme, and that's always nice...


Don't really now if this will help you with the problem, but that's the general approach you'd take without a framework. I'm not to versed in YII to guarantee it'll work here, but I see no reason why not.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...