2017-05-10 10:17
浏览 61

Doctrine querybuilder如何用参数数组替换参数?

I would like to replace some parameters by an array of parameters, because I don't know how many variables I will receive. The problem is that my function return some results that doesn't correspond to my origin array... And that's logic. I don't know how to do that.

I need to transform this function by an array of $filter instead of $filter, $filter2, $filter3.

public function getContentByFilterTest($categ,$filter,$filter2,$filter3){
    $query = $this->createQueryBuilder('c')
    ->join('c.filterfilter', 'f1')
    ->join('c.filterfilter', 'f2')
    ->join('c.filterfilter', 'f3')
    ->andWhere('f1.idfilter = :filter_idfilter1')
    ->andWhere('f2.idfilter = :filter_idfilter2')
    ->andWhere('f3.idfilter = :filter_idfilter3')
    ->andWhere('c.contentCategorycontentCategory = :category')
            'filter_idfilter1' => $filter,
            'filter_idfilter2' => $filter2,
            'filter_idfilter3' => $filter3,
            'category' => $categ,
    return $query;

I tried this :

public function getContentByFilterTestBoucle($categ, array $filters ){
    $query = $this->createQueryBuilder('c')->select('');
    for ($i = 1; $i <= count($filters); $i++){
        $query = $query->join('c.filterfilter', 'f'.$i)
        ->andWhere('f'.$i.'.idfilter = :filter_idfilter'.$i)
                'filter_idfilter'.$i => $filters[$i-1],
    $query->andWhere('c.contentCategorycontentCategory = :category')
    ->setParameter('category', $categ)
    return $query;
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题