简体   繁体   English

我应该从DBIx :: Class :: ResultSet :: *方法返回什么结果?

[英]What result should I return from DBIx::Class::ResultSet::* methods?

I have next code: 我有下一个代码:

#find contact with given email and/or phone
sub contact {
    my( $self, $email, $phone ) =  @_;

    my $user =  $self;
    $user =  $user->search({ email => $email })   if $email;
    $user =  $user->search({ phone => $phone })   if $phone;

    return $user->first; # I think this is wrong
}

It it OK that in my ArtCoin::Schema::ResultSet::User package to return Result ? 我可以在ArtCoin::Schema::ResultSet::User包中返回Result吗?

When you build a custom ResultSet, you typically return the new ResultSet object. 构建自定义ResultSet时,通常会返回新的ResultSet对象。 In the documentation the examples contain an implicit return . 文档中 ,示例包含隐式return

 sub active { my $self = shift; $self->search({ $self->current_source_alias . '.active' => 1 }); } 

Because there is no return , it just returns the return value of the last statement. 因为没有return ,所以它只返回最后一条语句的返回值。 That's the $self->search(...) part. 那是$self->search(...)部分。

So your code would be like this: 因此,您的代码将如下所示:

sub contact {
    my( $self, $email, $phone ) =  @_;

    my $user =  $self;
    $user =  $user->search({ email => $email })   if $email;
    $user =  $user->search({ phone => $phone })   if $phone;

    return $user;
}

Remember that the ResultSet is not actually a result. 请记住,ResultSet实际上不是结果。 There has not been a query at that point. 那时还没有查询。 If you call ->first on it in your method, it will talk to the database and run your query. 如果您在方法中->first调用->first它,它将与数据库对话并运行查询。 After that, you cannot chain additional search es to the ResultSet, because you don't have a ResultSet any more. 之后,您将无法将其他search到ResultSet,因为您再也没有ResultSet了。


The following section is a bit of code-review. 以下部分是一些代码审查。

What you did there works quite well, but it's a bit complex. 您在这里所做的工作效果很好,但是有点复杂。 You are chaining up to two additional searches, which translate to more stuff that DBIC needs to do for you in the background. 您将链接最多两个额外的搜索,这将转化为DBIC在后台需要为您做的更多工作。 Think about how that method should behave. 考虑一下该方法应如何表现。

Right now, it does this: 现在,它执行以下操作:

  • if there are no arguments $rs->contact() , it will return the same ResultSet as before, without any new search criteria ( SELECT * FROM contacts ) 如果没有参数$rs->contact() ,它将返回与以前相同的ResultSet,而没有任何新的搜索条件( SELECT * FROM contacts
  • if there was the email argument $rs->contact($email) , it will return a new ResultSet that has an additional search criteria for the email ( SELECT * FROM contacts WHERE email='...' ) 如果有电子邮件参数$rs->contact($email) ,它将返回一个新的ResultSet,其中包含该电子邮件的其他搜索条件( SELECT * FROM contacts WHERE email='...'
  • if there was the phone argument $rs->contacts( undef, $phone ) , it will return a new ResultSet that has an additional search criteria for the phone ( SELECT * FROM contacts WHERE phone='...' ) 如果有电话参数$rs->contacts( undef, $phone ) ,它将返回一个新的ResultSet,其中包含电话的其他搜索条件( SELECT * FROM contacts WHERE phone='...'
  • if there were both email and phone arguments $rs->contacts( $email, $phone ) , it will return a new ResultSet that has two additional search criteria ( SELECT * FROM contacts WHERE email='...' AND phone='...' ) 如果同时有电子邮件和电话参数$rs->contacts( $email, $phone ) ,它将返回一个新的ResultSet,它具有两个附加的搜索条件( SELECT * FROM contacts WHERE email='...' AND phone='...'

That makes sense, but for the first case. 这是有道理的,但对于第一种情况。 If you don't want any narrowing down, you'd not call that method in the first place, would you? 如果您不希望缩小范围,那么首先不要调用该方法,对吗?

That depends on what you need. 这取决于您的需求。 I prefix all methods returning ResultSets with 'search_' and all that return a single object 'find_'. 我为所有返回ResultSets的方法添加“ search_”前缀,并为所有返回单个对象“ find_”的方法添加前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM