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

时间:2014-03-18 15:01:04

标签: perl moose dbix-class

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

这些是我的目标:

  • 关注Moose' Moose::Manual::BestPractices,尤指:
    • 使用namespace::autoclean
    • 使用__PACKAGE__->meta->make_immutable
  • 使用ResultResultSet
  • 的常用基类
  • 使用任何魔法技巧时都会有一条评论解释它们(在研究过程中,我发现了don't ask解释的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}}列并未充气

2 个答案:

答案 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 strictuse warnings,但没有大问题。 好的部分是,如果有任何变化,希望DBIx::Class::Schema::Loader将更新以说明任何所需的更改。

道歉,如果我错过了这一点。