简体   繁体   English

DBIx :: Class :: ResultSet问题

[英]DBIx::Class::ResultSet problems

I've got the following code: 我有以下代码:

package MyPackage::ResultSet::Case;
use base 'DBIx::Class::ResultSet';

sub cases_last_fourteen_days {
    my ($self, $username) = @_; 

    return $self->search({
                username    => $username,
                date        => { '>=' => 'DATE_SUB(CURDATE(),INTERVAL 14 DAY)' },
    }); 
};

But when I try to use it this way: 但是当我尝试以这种方式使用它时:

$schema->resultset('Case')->cases_last_fourteen_days($username)

I always get zero results, can anyone tell what I'm doing wrong? 我总是得到零结果,谁能说出我做错了什么?

Thanks! 谢谢!

The way you use the SQL::Abstract condition would result in this where condition: 您使用SQL :: Abstract条件的方式将导致以下条件:

WHERE username = ? AND date >= 'DATE_SUB(CURDATE(),INTERVAL 14 DAY)'

When you wish to use database functions in a where clause you need to use a reference to a scalar , like this: 当您希望在where子句中使用数据库函数时,您需要使用对标量的引用 ,如下所示:

date        => { '>=' => \'DATE_SUB(CURDATE(),INTERVAL 14 DAY)' },

ProTip : if you set the environment variable DBIC_TRACE to 1, DBIx::Class will print the queries it generates to STDERR ... this way you can check if it really does what you wish. ProTip :如果你将环境变量DBIC_TRACE设置为1,DBIx :: Class会将它生成的查询打印到STDERR ......这样你就可以检查它是否真的符合你的意愿。

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

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