[CDBI] re-casting cdbi classes?

Dave Howorth dhoworth at mrc-lmb.cam.ac.uk
Mon Mar 20 15:50:14 GMT 2006

Ben Lavender wrote:
> Hi all,
> I've come across a situation where a client needs a change in
> requirements which would, ideally, require a slight slight breaking
> out of one class into a base class and two sub classes.  Put shortly,
> I have an event which is based on due dates right now, but some
> classes of events are turning up which need to be done based on
> mileage instead.  Thus, I previously had:
> Package Event;
> ...
> __PACKAGE__->columns(Essential => qw/ ... dueDate .../)
> I've got a lot of logic which touches lots of different fields based
> on these events, so my solution to the problem, if possible, would be
> as follows.  The database schema is already fixed up to accomidate
> this; I basically added usesDates, usesMiles, and dueMileage fields to
> the table to store the difference in the two classes.  This solution
> seems to be the best for my situation; a fair rundown I used to help
> me on this situation is at
> http://www.agiledata.org/essays/mappingObjects.html#MappingInheritance.
> Package Event;
> .
> .
> __PACKAGE__->columns(Essential => qw/ ..dueDate ... usesDates
> usesMiles dueMileage/);
> *various generic methods here*
> ######
> Package DateEvent
> use base Event;
> __PACKAGE__->columns(Essential => qw/... dueDate ... usesDates usesMiles/);
> *various subs related to dates*
> ######
> Package MilesEvent
> use base Event;
> __PACKAGE__->columns(Essential => qw/ ... ... usesDates usesMiles dueMileage/);
> *various subs related to mileage*
> I've already been playing with this a bit, and part of it works well
> and part of it doesn't.  The problem is this point:
> $event = Event->retrieve($id);
> &some_generic_event_stuff($event);
> if ($event->usesMiles) {
>     &some_specific_miles_stuff($event);  #Crap!
> }
> The problem is that I dont see a way to tell this class that it should

I take it you mean object?                          ^^^^^

> be a mileage event now instead of a generic one at the last method
> call without just reloading it from the database;

Can't you just bless it into the appropriate class?

Cheers, Dave

More information about the ClassDBI mailing list