我发现很难找到有关如何组装 DBIx::Class
的信息。使用 Moose
的模式结构.如何正确地做到这一点(基本上工作)和现代 Perl(良好的风格,快速,没有警告)?
这些是我的目标:
Moose::Manual::BestPractices
, 尤其:namespace::autoclean
和 __PACKAGE__->meta->make_immutable
. Result
的通用基类和 ResultSet
sub BUILDARGS { $_[2] }
由 don't ask 解释的指南)MooseX::NonMoose
(如有必要)或 __PACKAGE__->load_components
, 按照 DBIx::Class::Manual::Cookbook
的建议进入公共(public)基类这些是我遇到的问题:
__PACKAGE__->meta->make_immutable
时我收到了类似 Not inlining 'new' for MyApp::Schema::Result::MyTable since it is not inheriting the default Moose::Object::new
的警告__PACKAGE__->load_components
时到Result
基类我的datetime
列没有膨胀 最佳答案
出现的问题的解决方案:
make_immutable
与非 Moose
冲突new
构造函数:由 use MooseX::NonMoose
自动处理;与其文档相比,不需要进一步的论据或选项;当心 DBIx::Class::Schema
没有new
方法,因此,MyApp::Schema
不是 需要这个 helper InflateColumn::DateTime
在基类中加载时不膨胀:这是由赋予 load_components()
的组件顺序触发的;文档中没有提示顺序应该很重要,我已提交 a bug report对这个;重新排序有帮助 上面的解决方案包括我的带有 Moose 的示例 DBIx::Class 模式如下所示:
架构类:
package MyApp::Schema;
use Moose; # we want Moose
use MooseX::MarkAsMethods autoclean => 1; # this helps removing unused symbols like Moose keywords
# do NOT 'use MooseX::NonMoose' here because Schema class has no 'new' method
extends 'DBIx::Class::Schema'; # the Moose way of inheritance
# load all table modules automatically
__PACKAGE__->load_namespaces(
# ResultSet class for tables without custom ResultSet class
# (would be DBIx::Class::ResultSet otherwise)
default_resultset_class => '+MyApp::Schema::ResultSet',
);
# tell Moose this class is finished: some Moose stuff is removed and things go faster
__PACKAGE__->meta->make_immutable;
1;
常见
Result
基类:# a base class for all table class of this app
package MyApp::Schema::Result;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
use MooseX::NonMoose; # this is important for correctly handling DBIx::Class' new
extends 'DBIx::Class::Core';
# this is the right place to implement generic stuff
# DBIx::Class::Cookbook recommends loading components in a central place
__PACKAGE__->load_components(qw/
InflateColumn::DateTime
...
/);
__PACKAGE__->meta->make_immutable;
1;
常见
ResultSet
基类:package MyApp::Schema::ResultSet;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
use MooseX::NonMoose;
extends 'DBIx::Class::ResultSet';
__PACKAGE__->meta->make_immutable;
1;
示例
ResultSet
表类 my_table
: package MyApp::Schema::ResultSet::MyTable;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'MyApp::Schema::ResultSet';
sub oldest {
my $self = shift;
$self->search({}, {order_by => {-ASC => 'date'}})->first;
}
__PACKAGE__->meta->make_immutable;
1;
示例
Result
表类 my_table
: package MyApp::Schema::Result::MyTable;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'MyApp::Schema::Result';
__PACKAGE__->table("my_table");
__PACKAGE__->add_columns(
id => {data_type => "integer", is_auto_increment => 1},
date => {data_type => "date"},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->meta->make_immutable;
1;
关于perl - 如何使用 Moose 设置 DBIx::Class 模式——明确指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22483145/