Re: Class::DBI::Loader and accessors
[prev]
[thread]
[next]
[Date index for 2005/03/16]
Kingsley Kerce writes:
> Perrin Harkins writes:
> > Isn't this what the "additional_base_classes" parameter in Loader is
> > for?
>
> Well, yeah, if you're running CDBIL >= 0.06, unlike me. :)
> I'll upgrade. Thanks for the reality check, Perrin.
Using Class::DBI::Loader 0.17 and the additional*_classes params, I've
managed to eliminate looping/eval'ing over all my CDBI table classes
in order to include my base class My::DBI and the plugin class
Class::DBI::Plugin::RetrieveAll.
But I can't find a way to move some add_relationship_type() and
columns() invocations -- which must be done for every table class --
into my base class. Appended below is my app-initialization code,
before and after using CDBIL 0.17.
Looking at the second chunk of code below, is there a way to eliminate
looping over the table classes, and move the add_relationship_type()
and columns() calls into my base class, such that those calls will
occur during the CDBIL phase?
Thanks,
Kings
#------------------------------------------------------------------------------
#-- OLD METHOD, USING Class::DBI::Loader 0.03 ---------------------------------
#------------------------------------------------------------------------------
my $loader = Class::DBI::Loader->new(
dsn => $data_source,
user => $db_user,
password => $db_pass,
namespace => "My",
options => {
# ...
}
));
# We do not use triggers in this application. In particular, we want
# PostgreSQL to cascade deletions rather than having Class::DBI do it.
eval "package Class::DBI::Relationship::HasMany::NoCascadeDelete;
use base 'Class::DBI::Relationship::HasMany';
sub triggers { return () }
";
confess $@ if $@;
foreach my $class ($loader->classes) {
eval "package $class;
use base 'My::DBI';
use Class::DBI::Plugin::RetrieveAll;
__PACKAGE__->add_relationship_type(has_many => 'Class::DBI::Relationship::HasMany::NoCascadeDelete');
if ('$class' =~ /Type\$/) {
# May as well pull in all columns of db tables named %_type
__PACKAGE__->columns(Essential => __PACKAGE__->columns);
}
";
confess $@ if $@;
}
#------------------------------------------------------------------------------
#-- IMPROVED METHOD, USING Class::DBI::Loader 0.17 ----------------------------
#------------------------------------------------------------------------------
my $loader(Class::DBI::Loader->new(
dsn => $data_source,
user => $db_user,
password => $db_pass,
namespace => "My",
additional_classes => qw/Class::DBI::Plugin::RetrieveAll/,
additional_base_classes => qw/My::DBI/,
options => {
# ...
}
));
# We do not use triggers in this application. In particular, we want
# PostgreSQL to cascade deletions rather than having Class::DBI do it.
eval "package Class::DBI::Relationship::HasMany::NoCascadeDelete;
use base 'Class::DBI::Relationship::HasMany';
sub triggers { return () }
";
confess $@ if $@;
foreach my $class ($loader->classes) {
$class->add_relationship_type(has_many => 'Class::DBI::Relationship::HasMany::NoCascadeDelete');
if ($class =~ /Type$/) {
# May as well pull in all columns of db tables named %_type
$class->columns(Essential => $class->columns);
}
}
#------------------------------------------------------------------------------
|
(message missing)
|