如何使用Moose设置DBIx :: Class模式 - 明确指南

Dan*_*mer 7 perl moose dbix-class

我发现很难找到有关如何使用组装DBIx::Class模式结构的信息Moose.如何正确地(基本上工作)和现代Perl(良好的风格,快速,没有警告)?

这些是我的目标:

  • 跟随穆斯Moose::Manual::BestPractices,特别是:
    • 使用namespace::autoclean
    • __PACKAGE__->meta->make_immutable.
  • 使用普通的基类ResultResultSet
  • 当使用任何魔术技巧有一个解释他们的评论(在研究期间我发现一个指导建议sub 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_componentsResult基类时,我的datetime列没有被夸大

Dan*_*mer 7

出现的问题的解决方案:

  • 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)


mik*_*kew 5

这有什么错用use_mooseDBIx ::类::模式::装载机?(例如dbicdump -o use_moose=1 MyApp::Schema <dsn> <user> <pass>

这是use_moose=1DBIx::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)

这是创建Moosified ResultSet类的文档

看起来确实有些多余use strictuse warnings,但根本不算什么.好的部分是,如果有任何变化,希望DBIx::Class::Schema::Loader将更新以解释任何所需的变化.

如果我错过了重点,请道歉.