[英]Propel2 Build Query with functions and then call Find
在这里使用PHP的日子很少,而我却为我感到难过。 我正在使用Propel2根据用户选择的过滤器从数据库中检索行。 最终,我希望用户可以选择很多过滤器,然后生成一个自定义的Propel2调用以检索记录。 由于可能查询的数量成倍增加,因此我无法使用If / Then。 但是,以下示例中的每种方法对我来说都是失败的。
$dealfinder = DealsQuery::create()->filterByStillvalid(1)->orderByInception('DESC');
if(isset($dept)) {
$dealfinder->filterByCategory($dept);
}
if (isset($early)) {
$oneHourAgo = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'))- $dealDelay);
$dealfinder->filterByInception(array('max' => $oneHourAgo ));
}
$dealfinder->paginate($page = $pageNum, $maxPerPage = $maxColumn*$maxRow);
上面的代码返回:
deals.stillvalid=:p1, deals.inception<=:p1
这可能只是PHP函数链接语法的事情,所以这是一个有效的Propel2调用的样子:
$dealfinder = DealsQuery::create()->filterByStillvalid(1)->filterByCategory($dept)
->orderByInception('DESC')->filterByInception(array('max' => $oneHourAgo ))
->paginate($page = $pageNum, $maxPerPage = $maxColumn*$maxRow);
我将非常感谢您的帮助。 谢谢。
我遇到了类似的问题,最终在if语句内移动了初始查询。 除非有更清洁的解决方案,否则它可能不是最好的解决方案,但它为我解决了问题。
if(isset($dept)) {
$dealfinder = DealsQuery::create()
->filterByCategory($dept)
->filterByStillvalid(1)
->orderByInception('DESC');
}
if (isset($early)) {
$oneHourAgo = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'))-$dealDelay);
$dealfinder = DealsQuery::create()
->filterByInception(array('max' => $oneHourAgo ))
->filterByStillvalid(1)
->orderByInception('DESC');
}
$dealfinder->paginate($page = $pageNum, $maxPerPage = $maxColumn*$maxRow);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.