[CDBI] Different subclasses for one table / Factory Pattern?

Oliver Jeeves oj at defuturo.co.uk
Mon Sep 11 16:03:54 BST 2006

Oliver Jeeves wrote:
> Oliver Jeeves wrote:
> ...
> However, this leads naturally to my next question; how do I achieve this
> sort of thing?
> Currently, I'm thinking of having the base class call retrieve on the
> derived class, which will probably mean extra DB lookups, but will
> definatly work. Does anyone have a better solution?
> -Oli

Episode IV: A New Hope

It's been awfully quite on this list recently, where has everyone gone?

Anyway, I think I now have a working solution; override the construct

Looking at the internals of CDBI, it appears that all the constructors
evenatually call construct (except, maybe, via iterators, but by this
point I had gone cross eyed, so I'll leave this to someone else to
check), so my approach is to override construct with a simple wrapper
that just calls construct on the correct class.

Initially, this caused an infinite loop as the I was calling construct
on a derived class which didn't define its own construct method. I got
around this by calling SUPER::construct unless the class the method was
called on was equal to __PACKAGE__.

So The code looks like this:

sub construct
	my $proto	= shift; ## class or obj
	my $data	= shift;
	my $class = ref $proto || $proto;
	if ($class eq __PACKAGE__)
		my $new_class =
			'My::Base::Class::' . $data->{'type'};
		return $new_class->construct($data);
		return $proto->SUPER::construct($data);

In my code, the (Essential) DB column 'type' determines which subclass
the object should be.

This means that my derived classes automatically inherit all the CDBI
relationships from the base class, and can also add their own
relationships, which you can't do if re-blessing a CDBI object after

I hope this is of help for anyone trying to do something similar, and I
hope that someone can point out any problems or potential pitfalls with
this approach before I get myself into serious trouble!


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.digitalcraftsmen.net/pipermail/classdbi/attachments/20060911/f85130bf/signature.pgp

More information about the ClassDBI mailing list