Search code examples
perlinheritancecatalystdbix-class

Perl with Catalyst and DBIx::Class - How to load a sets of classes in an inheritance tree?


I would like to have a base class Vehicle and most of my system to just use ARRAYs of this type. With subtypes such as Car, Airplane etc. When calling a method such as canFly a Car would responsed false, while an Airplane would response true.

I'm planning to use a single DB table (will listen to other suggestions) with a subtype column indicating the actual subclass type and NULL values for the columns used by other sibling subclasses. How can I at DB query intercept the creation of Vehicle classes and rather given(subclasstype) create the appropriate subclass? (If this question is not clear I will attempt to clarify better Saturday night (about 28 hrs from now.))


Solution

  • What you looking for is called Dynamic Subclassing in DBIx::Class. In Rails/ActiveRecord they call it Single Table Inheritance (STI).

    The page to which I've linked is in the DBIC Cookbook describes how to override inflate_result so that you can test your row's vehicle type, and then rebless it into the desired subclass.