Re: abstract class with Class::DBI
[prev]
[thread]
[next]
[Date index for 2004/10/21]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
As you are using Pg, you could use trhe table inheritance of the rdbms.
I don't know if CDBI works well with inherited tables, but it should be
possible to move all the magic into the rdbms, leading to much simpler code.
Edward Betts wrote:
| I've got a bunch of activities, they are of different types like e-mail,
| SMS, and registration. They are represented by an abstract class, called
| Activity, with subclasses for each type, called ActivityEmail,
ActivitySms,
| and ActivityReg. I'm trying to work out how to store them using
Class::DBI.
|
| I'm using PostgreSQL for the database, fields that are common to all the
| types are stored the activity table.
|
| create table activity (
| id serial primary key,
| title varchar(50),
| activitytype character(10) check (activitytype in ('email',
'sms', 'reg'));
| activitydate date,
| );
|
| create table activity_email (
| activity integer primary key references activity(id),
| subject varchar(80),
| body text,
| whofrom varchar(80),
| whoto varchar(80),
| );
|
| create table activity_sms (
| activity integer primary key references activity(id),
| message varchar(160)
| )
|
| create table activity_reg (
| activity integer primary key references activity(id),
| purpose varchar(15) check (purpose in ('General', 'Competition')),
| );
|
| For each activity there is one entry in the activity table, and one in the
| table for that specific type.
|
|
| Column definitions are provided by Class::DBI::Loader, here is the
| Class::DBI code to describe the classes:
|
| package MyApp::Activity;
|
| __PACKAGE__->might_have(activity_email => 'VV::ActivityEmail');
| __PACKAGE__->might_have(activity_sms => 'VV::ActivitySms');
| __PACKAGE__->might_have(activity_reg => 'VV::ActivityReg');
|
| package MyApp::ActivityEmail;
| use base qw(MyApp::Activity);
|
| __PACKAGE__->has_a(activity => 'MyApp::Activity');
|
| package MyApp::ActivitySms;
| use base qw(MyApp::Activity);
|
| __PACKAGE__->has_a(activity => 'MyApp::Activity');
|
| package MyApp::ActivityReg;
| use base qw(MyApp::Activity);
|
| __PACKAGE__->has_a(activity => 'MyApp::Activity');
|
|
| Does all of this look right, or should I be doing it another way?
- --
Grüße,
Andreas Fromm
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFBd16KUmBtSMq5cGURAhEcAKDGPvj9QtA1Sl+5wT+0sVSopfWsswCgv6dD
LIH3lemyr9fFyR+rCKDD5O8=
=2WZl
-----END PGP SIGNATURE-----
|
(message missing)
|