[英]DBIx::Class abstract parent ResultSet
I am trying to figure out a clean OO way to implement a problem I have run into with DBIx::Class. 我试图找出一种干净的OO方法来解决我在DBIx :: Class中遇到的问题。 I have a User table that contains information common to all users.
我有一个用户表,其中包含所有用户共有的信息。 Each user can also have many different classes each which has its own unique required information.
每个用户还可以具有许多不同的类,每个类都有其自己的唯一必需信息。 So for example a User may be an Admin and an Author.
因此,例如,用户可以是管理员和作者。 There are separate tables for the admin and author classes.
admin和author类有单独的表。
What I want to be able to do is create a common base class to access all of the classes from the user object. 我想要做的是创建一个通用的基类来访问用户对象中的所有类。 So a base class called Schema::UserClass and two subclasses called Schema::UserClass::Admin and Schema::UserClass::Author.
因此,一个名为Schema :: UserClass的基类和两个名为Schema :: UserClass :: Admin的子类和Schema :: UserClass :: Author。 What I would like to be able to do are things like:
我想做的是这样的事情:
# Get current user
my $user = MyApp->get_user();
# Get user classes
my @classes = $user->classes->all();
for my $class (@classes) {
# Print class name
print $class->name;
}
A similar problem is presented here: http://dbix-class.35028.n2.nabble.com/OO-advice-do-a-subclass-do-something-else-td5614176.html . 这里也存在类似的问题: http : //dbix-class.35028.n2.nabble.com/OO-advice-do-a-subclass-do-something-else-td5614176.html 。 But the solution is subpart in my opinion since it requires adding a new relationship for each class.
但是我认为该解决方案是次要的,因为它需要为每个类添加一个新的关系。
I don't see how a relationship can be made to the base class with knowledge of all of the sub classes. 我不知道如何利用所有子类的知识与基类建立关系。 Any help would be much appreciated.
任何帮助将非常感激。
The solution I found isn't great either but it does work. 我发现的解决方案也不是很好,但确实可以。 If I get time I may consolidate this code in a CPAN module to make it a bit prettier.
如果有时间,我可以将此代码整合到CPAN模块中,以使其更漂亮。
package ParentSchema::Result::Class;
use strict;
use warnings;
use parent 'DBIx::Class::Core';
__PACKAGE__->add_columns(
"user_id",
{
data_type => "integer",
size => 32,
is_foreign_key => 1,
is_auto_increment => 0,
is_nullable => 0,
default_value => '',
},
);
# stuff common to all schemas
__PACKAGE__->belongs_to(
"user",
"Schema::Result::User",
{ 'foreign.id' => "self.user_id" },
{ is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);
1;
package Schema::Result::Class::Author
use strict;
use warnings;
use parent 'ParentSchema::Class';
__PACKAGE__->table('class_author');
# class spesific stuff
__PACKAGE__->meta->make_immutable;
1;
package Schema::Result::User;
use strict;
use warnings;
use parent 'DBIx::Class::Core';
use Module::Pluggable::Object;
use String::CamelCase qw(decamelize);
__PACKAGE__->add_columns(
"id",
{
data_type => "integer",
size => 32,
is_auto_increment => 1,
is_nullable => 0,
default_value => '',
},
);
my $class_path = 'Schema::Result::Class';
my $mp = Module::Pluggable::Object->new(
search_path => [ $class_path ]
);
my @class_plugins = $mp->plugins;
foreach my $class (@class_plugins) {
(my $name = $class) =~ s/^\Q${class_path}\E//;
__PACKAGE__->might_have(
decamelize($name),
$class,
{ "foreign.user_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
}
__PACKAGE__->meta->make_immutable;
1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.