[Dbix-class] DBIx::Class::AccessorGroup
Dave Howorth
dhoworth at mrc-lmb.cam.ac.uk
Thu Mar 8 10:13:25 GMT 2007
I wrote:
> I'm trying to use DBIx::Class::AccessorGroup to make an accessor that
> trims field values, as discussed in another thread. Since I want to use
> this accessor with many tables, I want to define the code in some
> package that is used for all the tables. But I can't figure out where to
> put it and I'm getting confused about base classes.
>
> So for example I have a table NewDomainSegment and I have code like this:
>
> package SU::Schema::NewDomainSegment;
> use base 'SU::Schema';
>
> __PACKAGE__->load_components(qw/AccessorGroup Core/);
> __PACKAGE__->mk_group_ro_accessors( trim => qw/sprot_ac/ );
>
> and in SU::Schema, I have:
>
> sub get_trim
> {
> my ($self, $column) = @_;
>
> my $raw_value = $self->get_column($column);
> $raw_value =~ /^\s*(.*?)\s*$/;
> my $trimmed_value = $1;
> return $trimmed_value;
> }
>
> but it's not working; SU::Schema::NewDomainSegment->can('get_trim') is
> undefined. Is SU::Schema the right place to put the implementation of
> get_trim?
I created an extra class (SU::Shared) that just contains the
implementation of get_trim(). Then I added 'use base "SU::Shared";' to
SU::Schema::NewDomainSegment and I called
SU::Schema::NewDomainSegment->mk_group_ro_accessors(trim => 'sprot_ac');
in my application. That worked, but is there a better way?
I'm using DBIx::Class::Schema::Loader to generate the file for
SU::Schema::NewDomainSegment and I'd like to avoid having to modify the
files it creates, because the database schema changes from time to time
so I need to rerun Loader and I don't want to have to keep re-editing
the subclasses it generates.
Is there a way to tell DBIx::Class::Schema::Loader to include the use
base statement in the subclasses? Or some other place to implement the
method so it gets inherited automatically?
Thanks, Dave
More information about the Dbix-class
mailing list