我有太多的条件组合,可能会进入db调用,以至于我不可能为每个条件单独进行调用。我会有太多的if/else语句。
因此,我希望将条件推入数组中,以动态构建条件语句和传入的值。所以:
$cond_values = array();
$cond_values[] = $lender->id;
$cond_string = "lender_id = ?";
if (something) {
$cond_string .= " AND search_id =?";
$cond_values[] = $search_id;
}
if (something_else) {
$cond_string .= " AND cust_id =?";
$cond_values[] = $cust_id;
}
$matches = Match::all(array(
"select" => $select,
"conditions" => array($cond_string, $cond_values),
"order" => $order,
"joins" => $joins
));但只有当$cond_values中有0或1个元素时,这才有效。超过一个值,我得到一个“没有索引1的绑定参数”错误。它似乎只有在我这样做的情况下才能起作用:
$matches = Match::all(array(
"select" => $select,
"conditions" => array($cond_string, $cond_values[0],$cond_values[1] ),
"order" => $order,
"joins" => $joins
));但是值的数量会有所不同,所以我不能这样做。如果你有任何见解我将不胜感激。
https://stackoverflow.com/questions/44505129
复制相似问题