开发者

Change inheritance tree of DBIx Class Result classes?

开发者 https://www.devze.com 2023-03-15 15:07 出处:网络
G\'Day, I\'m 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 the Loader -- from:

G'Day,

I'm 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 the 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:

package BaseMooseDBI;

use Moose;

use base qw(DBIx::Class);

However, this does not s开发者_开发技巧eem to work at all, and it doesn't seem to inherit stuff from BaseMooseDBI package (attributes, etc.) I tried overriding load_components in BaseMooseDBI as well, but it never gets called - instead it errors that it cannot find load_components?

What seems to be the problem?

Note: I cannot use the newer use_moose and result_base_class when generating the result classes.

EDIT: Found the solution. Saw the how DBIx::Class::Schema::Loader does it now, have Mutable and Immutable result classes.


If you just want to add a few methods, etc. to the parent class, your code should work. You might need to use MooseX::NonMoose, and in the past I have had the parent subclass DBIx::Class::Core instead of DBIx::Class. Here is what I've 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 DBIx::Class specific information (ie, call __PACKAGE->table, __PACKAGE__->add_columns, etc) take a look at DBIx::Class::Helper::Row::SubClass. Using it, you define the parent class as you would a normal 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 Loader to auto-generate some of this code.

0

精彩评论

暂无评论...
验证码 换一张
取 消