Can you use inheritance in Class::DBI to make classes which use other Class::DBI classes to hold part of their data and implementation?

Consider the classic example of the classes for Person, Employee, and Boss. Suppose that Person holds the name and address, Employee holds the pay rate and department the employee works in, and Boss holds the department being bossed around and the bonus that boss will get at the end of the year if her department works hard enough. And suppose that for inheritance, a Boss is an employee, and an employee is a person.

Is it possible to use Class::DBI to represent the class hierarchy of Person / Employee / Boss? I've tried it out, and I can make it so that an Employee is a Person, by using use base 'Person'; and I can create a Person object by inserting an Employee object. That lets me store base class Person stuff for an employee.

However, if I change the table for the Employee to the employee table, Employee->table('employee'); then the base class data for Person, name and address, is no longer in the employee table. That means that Class::DBI thinks that the columns for name and address don't exist in the Employee class's table, because of course they don't.

So that's where I'm stuck, how can I use Class::DBI to make a class hierarchy using inheritance? Is it possible for a derived class to get data from its base class? Or is doing this impossible because of the inherent limitations of the underlying SQL layer?

As a workaround, I think I could say that an Employee "has a" person, such that in the employee table there would be a column for 'pid' which references which row in the person table is the person for this employee: Employee->has_a(pid => 'Person'); Then if I had an object of that class I could do something like this: $employee->pid->name = 'homer'; This seems like an appropriate SQL way to do it, but in OOP terms, it seems like a dirty hack.

Has anybody worked out how to represent inherited class hierarchies in Class::DBI? I'd really like to see an example please if somebody has!


In reply to Inheritance and class hierarchies in Class::DBI by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.