我发现很难找到有关如何使用DBIx::Class
汇总Moose
架构结构的信息。如何正确地(基本上工作)和现代Perl(良好的风格,快速,没有警告)?
这些是我的目标:
Moose::Manual::BestPractices
,尤指:
namespace::autoclean
和__PACKAGE__->meta->make_immutable
。Result
和ResultSet
sub BUILDARGS { $_[2] }
指南)MooseX::NonMoose
__PACKAGE__->load_components
(如有必要)或__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
基类时,我的{{1}}列并未充气答案 0 :(得分:7)
出现问题的解决方案:
make_immutable
与非Moose
new
构造函数冲突:这由use MooseX::NonMoose
自动处理;与其文件相比,不需要进一步的论据或选择;请注意DBIx::Class::Schema
没有new
方法,因此MyApp::Schema
不需要此帮助InflateColumn::DateTime
在基类中加载时不膨胀:这是由赋予load_components()
的组件顺序触发的;文档中没有任何暗示,订单应该重要,我已经就此提交了a bug report;重新排序帮助通过上面的解决方案,我的示例DBIx :: Class架构与Moose看起来像这样:
架构类:
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;
答案 1 :(得分:5)
在DBIx::Class::Schema::Loader中使用use_moose
有什么问题? (例如dbicdump -o use_moose=1 MyApp::Schema <dsn> <user> <pass>
这是use_moose=1
0.07039为架构生成的DBIx::Class::Schema::Loader
:
use utf8;
package MyApp::Schema;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces;
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-03-19 22:50:18
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:7Hx1RMeFsxCqo5YaLOzPdQ
# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
1;
结果类:
use utf8;
package MyApp::Schema::Result::Example;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
use strict;
use warnings;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';
...yadda...
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-03-19 22:50:18
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:yTmu6Rh9TAEwxqgDClBdtg
# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;
这里是creating a Moosified ResultSet class
的文档看起来确实有一些无关的use strict
和use warnings
,但没有大问题。
好的部分是,如果有任何变化,希望DBIx::Class::Schema::Loader
将更新以说明任何所需的更改。
道歉,如果我错过了这一点。