Propel2使用函数构建查询,然后调用查找

时间:2017-07-24 21:59:37

标签: php propel2

这里很少有几天进入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);

我非常感谢你的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,最终在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);