更改 DBIx 类结果类的继承树?

发布于 2024-11-16 20:42:45 字数 1039 浏览 1 评论 0原文

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_mooseresult_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-11-23 20:42:45

如果您只想向父类添加一些方法等,您的代码应该可以工作。您可能需要使用 MooseX::NonMoose,过去我使用父子类 DBIx::Class::Core 而不是 DBIx::Class。这是我成功使用的:

# 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();
}

如果您希望父类定义DBIx::Class特定信息(即调用__PACKAGE->table__PACKAGE__->add_columns 等)看看 DBIx::Class::Helper::Row::SubClass。使用它,您可以像普通的 DBIx::Class::Result::* 一样定义父类,并在子类中使用 SubClass 组件并调用 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

我不确定您是否可以让 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文