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

标签 perl moose dbix-class

我发现很难找到有关如何组装 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/

    相关文章:

    regex - perl,将字符附加到第i个捕获组

    perl - 我所有的 Moose 类都必须包含 'namespace::autoclean' 和 'make_immutable' 还是有某种方法可以默认获取这些?

    perl - “modern” Perl入门资源

    postgresql - 我可以在连接后定义 DBIx::Class::ResultSource::View 吗?

    perl - DBIx::Class 可以与存储过程而不是表一起使用吗?

    database - Catalyst 中的 DBIx::Class 升级(DBIx::Class::Schema::Loader)

    file - Perl - 在制表符分隔的文本文件中拆分列并用新值替换列的问题

    perl - 如何确定 Perl 文件句柄是读句柄还是写句柄?

    perl - Mojolicious::Lite 与模板工具包

    perl - 如何在 Moose 中声明/使用静态成员?