Dan*_*mer 7 perl moose dbix-class
我发现很难找到有关如何使用组装DBIx::Class模式结构的信息Moose.如何正确地(基本上工作)和现代Perl(良好的风格,快速,没有警告)?
这些是我的目标:
Moose::Manual::BestPractices,特别是:
namespace::autoclean和__PACKAGE__->meta->make_immutable.Result和ResultSetsub BUILDARGS { $_[2] }解释不要问)MooseX::NonMoose(如果需要)或者__PACKAGE__->load_components按照建议移动到公共基类中DBIx::Class::Manual::Cookbook这些是我遇到的问题:
__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也不会需要这个帮手InflateColumn::DateTime在基类中加载时不膨胀:这是由给定的组件顺序触发的load_components(); 文档中没有任何暗示,订单应该重要,我已经提交了关于此的错误报告 ; 重新排序有帮助上面的解决方案包括我的示例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;
Run Code Online (Sandbox Code Playgroud)
通用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;
Run Code Online (Sandbox Code Playgroud)
通用ResultSet基类:
package MyApp::Schema::ResultSet;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
use MooseX::NonMoose;
extends 'DBIx::Class::ResultSet';
__PACKAGE__->meta->make_immutable;
1;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
这有什么错用use_moose在DBIx ::类::模式::装载机?(例如dbicdump -o use_moose=1 MyApp::Schema <dsn> <user> <pass>
这是use_moose=1在DBIx::Class::Schema::Loader0.07039的模式产生:
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;
Run Code Online (Sandbox Code Playgroud)
结果类:
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;
Run Code Online (Sandbox Code Playgroud)
看起来确实有些多余use strict和use warnings,但根本不算什么.好的部分是,如果有任何变化,希望DBIx::Class::Schema::Loader将更新以解释任何所需的变化.
如果我错过了重点,请道歉.