2

我在 SQLite 数据库中有一个表,并且我有一个名为 Account 的模式。我还有一个搜索用户的表单。在我的控制器中,我这样做:

my $params ||= $c->req->parameters; # Don't know what the difference is between ||= and =, did that way because someone had done it to and it works
my $search_term = $params->{search};

if( $c->request->params{submit} )
{
    $c->stash->{search_term} = $search_term;

    $c->stash->{search_results} = $c->model("DB::Account")->find({
        firstname => { like => "%$search_term%" },
    }) if $c->stash->{search_term};
}

然后在我看来,我有这样的事情。

[% IF search_results > 0 %]
  [% FOREACH results IN search_results %]
    [% results.email %] <br/>
    [% results.firstname %]
[% END %]

上面的代码工作正常。我想要做的是搜索与 或其他列匹配的表firstnamelastname而不仅仅是一列。

我对 Catalyst 框架和 Perl 相当陌生(用 perl 做过一些事情,但不是很先进),所以任何帮助和提示将不胜感激。

4

1 回答 1

2

至于这个:

my $params ||= $c->req->parameters; 

相同:

my $params = $params || $c->req->parameters; # when $params is not defined 
                                             # it will be used the part after ||

至于从表中搜索行。该方法find将只找到一行。

$c->stash->{search_results} = $c->model("DB::Account")->find({
        firstname => { like => "%$search_term%" },            
});

为了找到所有行,您应该使用方法searchsearch_rs. 像这样的东西:

$c->stash->{search_results} = $c->model("DB::Account")->search({
        firstname => { like => "%$search_term%" },
        lastname => "Smith",
        email => "jsmith@abc.de"
})

就这样!有关更多信息,您必须阅读DBIx::Class::ResultSet

UPD

为了在你的 tt 文件中查看结果,你应该使用这个:

[% WHILE (row = search_results.next) %]
[% row.email %]
[% row.firstname %]
[% END %]
于 2013-04-18T19:20:26.923 回答