DBIX::Class 子class 结果 class
DBIX::Class subclassing result class
我正在尝试对 DBIX::Class 中的多个结果 class 使用公共基数 class。原因是 - 我有几个结构相同但名称不同的表。
这是我的基地class
use utf8;
package myapp::Schema::tablebase;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table("unknown");
__PACKAGE__->add_columns(
"id",
{ data_type => "smallint", is_nullable => 0 }
#, ... and lot more
);
这是实际结果class
package myapp::Schema::Result::ActualTable;
use base 'myapp::Schema::tablebase';
# Correct table name
__PACKAGE__->table('patient2');
1;
我遇到了编译错误。请帮我解决这个问题。
更新:
我收到的错误是 -
DBIx::Class::Schema::catch {...} (): 尝试 load_namespaces() class myapp::Schema::Result::ActualTable 失败 - 你确定这是一个真正的结果类?:无法通过包 "myapp::Schema::Result::ActualTable" 在 C:/Strawberry/perl/site/lib/DBIx/Class/Schema.pm 第 195 行找到对象方法 "result_source_instance"。在 C:/Strawberry/perl/site/lib/myapp/Schema.pm
这应该行得通,也许是因为您的基础 class 末尾缺少真正的 return 值 (1;)?
如果您更喜欢更简洁的解决方案,也可以使用 DBIx::Class::Helper::Row::SubClass 解决您的基础 class 可能已经定义的关系。
以下是我对方法 "subclass" 和 "generate_relationships" 所做的更改,以保留全局表和客户端特定表之间的各种关系。此外,我还必须从全局到许多客户端特定表中删除反向关系。
sub subclass {
my $self = shift;
my $client_id = shift;
$self->set_table($client_id);
$self->generate_relationships($client_id);
}
sub generate_relationships {
my $self = shift;
my $client_id = shift;
my ($namespace) = get_namespace_parts($self);
foreach my $rel ($self->relationships) {
my $rel_info = $self->relationship_info($rel);
my $class = $rel_info->{class};
assert_similar_namespaces($self, $class);
my (undef, $result) = get_namespace_parts($class);
eval "require $class";
# relation of self with global table e.g. person to zipcode or guarantor2 to person
# Copy relation as is
if($class->is_global == 1){
$self->add_relationship(
$rel,
"${namespace}::$result",
$rel_info->{cond},
$rel_info->{attrs}
);
}else{
# relation with client specific table e.g. patient2 has many guarantor2, person has many guarantor2
# skip if self is global ( person has many guarantor2/3/4 etc)
if($client_id ne ''){
# need client id mention in result class with which self is related
$self->add_relationship(
$rel,
"${namespace}::$result"."$client_id",
$rel_info->{cond},
$rel_info->{attrs}
);
}
}
};
}
对于每个子类,我将 client_id 作为参数传递,这对于全局表是空白的。
我正在尝试对 DBIX::Class 中的多个结果 class 使用公共基数 class。原因是 - 我有几个结构相同但名称不同的表。
这是我的基地class
use utf8;
package myapp::Schema::tablebase;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table("unknown");
__PACKAGE__->add_columns(
"id",
{ data_type => "smallint", is_nullable => 0 }
#, ... and lot more
);
这是实际结果class
package myapp::Schema::Result::ActualTable;
use base 'myapp::Schema::tablebase';
# Correct table name
__PACKAGE__->table('patient2');
1;
我遇到了编译错误。请帮我解决这个问题。
更新:
我收到的错误是 -
DBIx::Class::Schema::catch {...} (): 尝试 load_namespaces() class myapp::Schema::Result::ActualTable 失败 - 你确定这是一个真正的结果类?:无法通过包 "myapp::Schema::Result::ActualTable" 在 C:/Strawberry/perl/site/lib/DBIx/Class/Schema.pm 第 195 行找到对象方法 "result_source_instance"。在 C:/Strawberry/perl/site/lib/myapp/Schema.pm
这应该行得通,也许是因为您的基础 class 末尾缺少真正的 return 值 (1;)?
如果您更喜欢更简洁的解决方案,也可以使用 DBIx::Class::Helper::Row::SubClass 解决您的基础 class 可能已经定义的关系。
以下是我对方法 "subclass" 和 "generate_relationships" 所做的更改,以保留全局表和客户端特定表之间的各种关系。此外,我还必须从全局到许多客户端特定表中删除反向关系。
sub subclass {
my $self = shift;
my $client_id = shift;
$self->set_table($client_id);
$self->generate_relationships($client_id);
}
sub generate_relationships {
my $self = shift;
my $client_id = shift;
my ($namespace) = get_namespace_parts($self);
foreach my $rel ($self->relationships) {
my $rel_info = $self->relationship_info($rel);
my $class = $rel_info->{class};
assert_similar_namespaces($self, $class);
my (undef, $result) = get_namespace_parts($class);
eval "require $class";
# relation of self with global table e.g. person to zipcode or guarantor2 to person
# Copy relation as is
if($class->is_global == 1){
$self->add_relationship(
$rel,
"${namespace}::$result",
$rel_info->{cond},
$rel_info->{attrs}
);
}else{
# relation with client specific table e.g. patient2 has many guarantor2, person has many guarantor2
# skip if self is global ( person has many guarantor2/3/4 etc)
if($client_id ne ''){
# need client id mention in result class with which self is related
$self->add_relationship(
$rel,
"${namespace}::$result"."$client_id",
$rel_info->{cond},
$rel_info->{attrs}
);
}
}
};
}
对于每个子类,我将 client_id 作为参数传递,这对于全局表是空白的。