[Dbix-class] load_classes and common base class

Ash Berlin ash_cpan at firemirror.com
Sat Apr 21 15:19:14 GMT 2007


Bill Moseley wrote:
> Ok, get ready for a bunch of beginner questions...
> 
> 
> What's the recommended was to setup a common base class for all the
> result sources (table classes)?  A base class where I can put methods
> that can be called on any table object.
> 
> Should I have each class inherit from a base class and then have that
> base class inherit from DBIx::Class?  Or specify both DBIx::Class and
> my base class in 'use base'?
> 
> I assume I can't use my base schema class (where I call load_classes)
> as my base class as is done with CDBI.  Although that would seem a
> natural place.
> 
> 
> I looked at compose_namespace(), but I'm not clear from the docs if
> that's what I need.
> 
> 
> 
> With CDBI I got tired or typing the same things in my table classes --
> plus table classes were useful in more than on project, so I set them
> up so I didn't type the namespace more than once.
> 
> So instead of:
> 
>     package My::App::Foo::Album;
>     use base 'My::App::Foo';
> 
>     __PACKAGE->table( 'album' );
>     __PACKAGE->has_many( tracks => 'My::App::Foo::Track' );
> 
> I just wrote:
> 
>     package My::App::Foo::Album;
> 
>     __PACKAGE__->table( 'album' );
>     __PACKAGE__->has_many( tracks => 'Track' );
> 
> Where my load_classes would setup the inheritance before loading the
> class, and relationship setup assumed the base class (a "+" prefix on
> a class would say to not prefix it automatically).
> 

The way I do things is;

package YourPreferences::Base::Schema::Tests::Result;

use strict;
use warnings;
use base 'DBIx::Class';

__PACKAGE__->load_components('Core');

sub table {
     my $self = shift;

     $self->next::method(@_);

     # stuff done for all classes
     # e.g.
     # $self->resultset_class('My::ResultSet');
}

1;

package YourPreferences::Schema::Tests::Foo::Result;

use strict;
use warnings;

use base 'YourPreferences::Base::Schema::Tests::Result';

__PACKAGE__->table('tests_foo_results');

# continue as normal


HTH
Ash



More information about the Dbix-class mailing list