在PHP ActiveRecord中使用命名占位符

在PHP ActiveRecord中使用命名占位符,php,phpactiverecord,flightphp,Php,Phpactiverecord,Flightphp,我正在使用FlightPHP和PHPActiveRecord开发一个API,它返回一个报告数组,可以选择通过url查询字符串参数进行过滤 $allowed=['author'=>'author\u id','owner'=>'owner\u id']; $options=构建选项(Flight::request(),$allowed); $reports=Report::all($options); 方法build_options使用所有查询选项创建数组,在本例中,WHERE子句删除不允许的列

我正在使用FlightPHP和PHPActiveRecord开发一个API,它返回一个报告数组,可以选择通过url查询字符串参数进行过滤

$allowed=['author'=>'author\u id','owner'=>'owner\u id'];
$options=构建选项(Flight::request(),$allowed);
$reports=Report::all($options);
方法build_options使用所有查询选项创建数组,在本例中,WHERE子句删除不允许的列并忽略未传递的参数

函数构建选项($request,$allowed){
$子句=数组映射(函数($column,$value){
返回“($column=:$value或:$value为NULL)”;
},$allowed,数组_键($allowed));
$where=内爆($AND');
$params=array\u intersect\u key($request->query->getData(),$allowed);
返回['conditions'=>[$where,$params]];
}
此函数创建一个WHERE子句,如:

(author_id = :author OR :author IS NULL) AND (owner_id = :owner OR :owner IS NULL)
但PHPActiveRecord似乎不接受命名占位符。他们的GitHub中唯一询问这一问题的是2010年的问题,并且已经关闭

是否有其他方法可以使用可选参数优雅地过滤查询

-编辑-

我已经开发了一个带有位置占位符的变通方法,它可以在没有命名占位符的情况下工作。我仍然认为带有命名占位符的版本看起来更干净、更容易理解

function build_options($request, $allowed) {
  $params = array_intersect_key($request->query->getData(), $allowed);
  $clauses = array_map(function($param) use ($allowed) {
    return "$allowed[$param] = ?";
  }, array_keys($params));
  $where = implode($clauses, ' AND ');
  return ['conditions' => array_merge([$where], array_values($params))];
}

使用位置占位符。更好的是,完全摆脱phpactiverecord的东西。位置占位符对于使用可选参数来说有点混乱。由于activerecord在幕后使用PDO,我不明白为什么会出现这种不兼容性。我发现activerecord非常方便,易于集成。我只是错过了这个功能。