在存储库中,我需要一个自定义的getALL查询,它根据数据数组检查项目。例如我需要能够发送$ params = ['type'=> ['type1','type2'],'username'=> $ username]但我目前只能发送一个参数,例如:$ params = ['type'=> 'type1','username'=> $用户名]。
在此getAll查询中添加数组(如上所述)的最佳方法是什么?:
public function getAllQuery($params = [])
{
$query = $this->createQueryBuilder('c');
if(count($params))
{
foreach($params as $param => $value)
{
//todo need to make it accept an array of parameters
if(in_array($param, $this->getClassMetadata()->getFieldNames()) && $param != 'deleted')
{
$query
->andWhere(sprintf('c.%1$s LIKE :%1$s', $param))
->setParameter($param, sprintf('%%%s%%', $value));
}
/*code already exists here for checking other parameters
outside of 'type' (e.g. 'username') */
}
}
return $query->getQuery();
}
答案 0 :(得分:0)
我无法发表评论,所以我会毫不犹豫地回答我已经理解了这个问题。你在等这样的事吗?在这种情况下,它比Doctrine更像PHP问题。
过去代码的结果:here。
$params = array( 'type' => array( 'type1', 'type2' ), 'username' => 'flavien');
$requiredFields = array('type');
if(count($params))
{
foreach($params as $param => $value)
{
if(in_array($param, $requiredFields) && $param != 'deleted')
{
if(is_array($value))
{
echo "My param: {$param}" . PHP_EOL;
for($i=0; $i < count($value); $i++) {
echo $value[$i] . PHP_EOL;
}
}
}
}
}