The Joys of Single Table Inheritance
August 19th, 2006
CakePHP started as a PHP port of the Rails framework (please read the sidebar if you are unfamiliar with Ruby on Rails) but has grown into its own, in my opinion, most excellent project. Cake has the benefit of being based on PHP which is still, I believe, the most popular web programming language out there. One thing that I have found Cake lacking in comparison to Ruby on Rails is Single Table Inheritance.
Ruby on Rails?
If you have been living under a rock for the past year, you have probably never heard of Ruby on Rails. The rest of you know that Rails is a web application development framework, built using the Ruby programming language, that makes the development of richly functional web apps so easy it is enjoyable. Personally, I have played with Ruby on Rails a bit and been very impressed with it. I do not, however, use it for any of my development. Why? Primarily because of the low number of web hosts that offer support for it. My own webhost, Dreamhost, does support it but I can never be sure that my client’s favourite host will.
Single Table Inheritance is a method of using one database table to hold the information of multiple objects. For example, if you had tutorials and blog entries that had basically the same fields and were going to be treated in basically the same manner, you could store them all in one table and use Single Table Inheritance to have them treated differently in your application. This is, in fact, what I have done with this site. (see my tutorial Implementing Single Table Inheritance in CakePHP for information on how to accomplish this with CakePHP).
You may be wondering why we would want to bother with this when it is just as easy to create two tables and scaffold them seperately. In this case, yes, you are correct. Let’s take a look at another example which might shed some more light on the power of Single Table Inheritance. Let’s take a look at a site like Digg. With this site there are two types of entry that can be viewed: those on the front page and those that are “Upcoming”. They are identical except for the fact that one has been given the “green-light” (in the case of Digg, an algorithm determines this based on popularity and time and any number of other factors) and the other has not. If we were creating a site like this using Ruby on Rails or CakePHP without Single Table Inheritance, we would have two almost identical tables and the process of “green-lighting” would involve deleting a post from one table (upcoming) and inserting it into another (current).
With Single Table Inheritance, however, we can have everything in one table with an added field which specifies the type of object we are dealing with: Upcoming or Current. In this setup, “green-lighting” is as simple as changing the type field on the record. So, with Single Table Inheritance we get the benefit of having everything in one table and the benefits of being able to programmatically treat each as a different object. We can have entirely different methods of displaying, adding, editing or deleting the two even though they are held in the same table.



October 7th, 2008 at 8:44 pm
Hi!
I want to extend my SQL knowledge.
I red really many SQL books and want to
read more about SQL for my work as mysql database manager.
What would you recommend?
Thanks,
Werutz
January 7th, 2009 at 11:43 am
What is bumburbia?
June 28th, 2009 at 7:08 pm
Hi,
Whilst your example is a good example of SingleTableInheritance, it is pretty much the only time it is appropriate. In fact, in your examples isn’t it more like a union type than a pair of types with a common ancestor?
It is nothing compared to the power of real inheritance hierarchies implemented using ClassTableInheritance.