更改 DBIx 类结果类的继承树?
G'Day,
我正在使用 DBIx::Class 0.07003 和 DBIx::Class::Schema::Loader 0.03009,我正在尝试将 Loader 生成的类的基类从: 更改
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");
为类似的内容:
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");
BaseMooseDBI 看起来像:
package BaseMooseDBI;
use Moose;
use base qw(DBIx::Class);
但是,这似乎根本不起作用,而且它似乎没有从 BaseMooseDBI
包继承东西(属性等)我也尝试覆盖 BaseMooseDBI
中的 load_components
,但它永远不会被调用 - 相反,它会错误地找不到 load_components
?
似乎有什么问题?
注意:生成结果类时,我无法使用较新的 use_moose
和 result_base_class
。
编辑:找到解决方案。现在了解 DBIx::Class::Schema::Loader 是如何实现的,具有可变和不可变结果类。
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 seem 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想向父类添加一些方法等,您的代码应该可以工作。您可能需要
使用 MooseX::NonMoose
,过去我使用父子类DBIx::Class::Core
而不是DBIx::Class
。这是我成功使用的:如果您希望父类定义
DBIx::Class
特定信息(即调用__PACKAGE->table
,__PACKAGE__->add_columns
等)看看DBIx::Class::Helper::Row::SubClass
。使用它,您可以像普通的DBIx::Class::Result::*
一样定义父类,并在子类中使用SubClass
组件并调用subclass
:我不确定您是否可以让
Loader
自动生成部分代码。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 subclassDBIx::Class::Core
instead ofDBIx::Class
. Here is what I've used successfully:If you want the parent class to define
DBIx::Class
specific information (ie, call__PACKAGE->table
,__PACKAGE__->add_columns
, etc) take a look atDBIx::Class::Helper::Row::SubClass
. Using it, you define the parent class as you would a normalDBIx::Class::Result::*
and in the child class use theSubClass
component and callsubclass
:I'm not sure if you can get
Loader
to auto-generate some of this code.