如何为Schema类提供DBIx搜索方法的附加参数

时间:2013-02-05 14:38:32

标签: mysql perl catalyst dbix-class

我在Catalyst项目中添加了与我的Schema的新关系:

__PACKAGE__->has_many(
    'messageslog' => "myproject::Schema::Result::MessagesLog",
    sub {
    my $args = shift;
    return {
        "$args->{foreign_alias}.from" => "$args->{self_alias}.msisdn",
        "$args->{foreign_alias}.service_id" => "3",
        "$args->{foreign_alias}.date_time" => {between => ["2013-01-01 00:00:00", "2013-   01-05 23:59:59"]}
        };
    }
);

当我提出请求时:

$c->model('DB::TableClass')->search({})->hashref_array;

效果很好。

但我需要插入这样的搜索参数:

__PACKAGE__->has_many(
    'messageslog' => "myproject::Schema::Result::MessagesLog",
    sub {
    my $args = shift;
    return {
        "$args->{foreign_alias}.from" => "$args->{self_alias}.msisdn",
        "$args->{foreign_alias}.service_id" => "$args->{params}->{id}",
        "$args->{foreign_alias}.date_time" => {between => $args->{params}->{dates}}
        };
    }
);
$c->model('DB::TableClass')->search({}, {
    join => 'messageslog',
    -params => {id => 3, dates => ["2013-01-01 00:00:00", "2013-01-05 23:59:59"]}
})->hashref_array;

我该怎么做?

1 个答案:

答案 0 :(得分:1)

通过向ResultSet添加方法而不是关系,请参阅Cookbook/Predefined-searches