Change DBIx Class Class Inheritance Tree?

G'day,

I am working with DBIx :: Class 0.07003 and DBIx :: Class :: Schema :: Loader 0.03009, and I'm trying to change the base class of the classes generated by Loader - from:

package S2S::DBIxperiment::Productions; # Created by DBIx::Class::Schema::Loader v0.03009 @ 2011-06-24 14:29:13 use base 'DBIx::Class'; __PACKAGE__->load_components("PK::Auto", "Core"); 

to something like:

 package S2S::DBIxperiment::Productions; # Created by DBIx::Class::Schema::Loader v0.03009 @ 2011-06-24 14:29:13 use base 'BaseMooseDBI'; __PACKAGE__->load_components("PK::Auto", "Core"); 

where BaseMooseDBI looks like this:

 package BaseMooseDBI; use Moose; use base qw(DBIx::Class); 

However, it does not seem to work at all, and it does not seem to inherit things from the BaseMooseDBI package (attributes, etc.). I tried to override load_components in BaseMooseDBI , but it never BaseMooseDBI called - instead, errors that it cannot find load_components ?

What is the problem?

Note. I cannot use the new use_moose and result_base_class when creating result classes.

EDITOR: Solution found. See how DBIx :: Class :: Schema :: Loader does it now, has the Mutable and Immutable classes.

+4
source share
1 answer

If you just want to add some methods, etc. to the parent class, your code should work. You might need to use MooseX::NonMoose , but in the past I had a parent subclass of DBIx::Class::Core instead of DBIx::Class . Here is what I used successfully:

 # Parent package App::Schema::Result; use Moose; use MooseX::NonMoose; use namespace::autoclean; extends 'DBIx::Class::Core'; sub parent_method { ... } # Child package App::Schema::Result::Product; use Moose; use MooseX::NonMoose; use namespace::autoclean; extends 'Keystone::Schema::Site::Result'; __PACKAGE__->table('products'); sub child_method { my ($self) = @_; $self->parent_method(); } 

If you want the parent class to define certain DBIx::Class (i.e., call __PACKAGE->table , __PACKAGE__->add_columns , etc.), see DBIx::Class::Helper::Row::SubClass . Using it, you define the parent class as a regular DBIx::Class::Result::* , and in the child class use the SubClass component and call SubClass :

 # Parent package App::Schema::Result::Parent; use Moose; use MooseX::NonMoose; extends 'DBIx::Class'; __PACKAGE__->load_components(qw{InflateColumn::DateTime Core}); __PACKAGE__->table('products'); ... # Child package App::Schema::Result::Child; use Moose; use MooseX::NonMoose; extends 'App::Schema::Result::Parent'; __PACKAGE__->load_components(qw{Helper::Row::SubClass Core}); __PACKAGE__->subclass; # Now add the child specific stuff / override parent stuff 

I'm not sure if you can get a Loader to automatically generate some code.

+1
source

All Articles