2

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')
    ->setParameters(array(
            'filter_idfilter1' => $filter,
            'filter_idfilter2' => $filter2,
            'filter_idfilter3' => $filter3,
            'category' => $categ,
    ))
    ->getQuery()->getResult();
    return $query;
}

I tried this :

public function getContentByFilterTestBoucle($categ, array $filters ){
    $query = $this->createQueryBuilder('c')->select('c.name');
    for ($i = 1; $i <= count($filters); $i++){
        $query = $query->join('c.filterfilter', 'f'.$i)
        ->andWhere('f'.$i.'.idfilter = :filter_idfilter'.$i)
        ->setParameters(array(
                'filter_idfilter'.$i => $filters[$i-1],
        ))
        ;
    }
    $query->andWhere('c.contentCategorycontentCategory = :category')
    ->setParameter('category', $categ)
    ->getQuery()->getResult();
    return $query;
}
1
  • You need to loop through your $filters and build your query accordingly as well as setting parameters. Commented May 10, 2017 at 10:29

1 Answer 1

3

The setParameters replace all previously setted parameter, so you shoud use this:

    ->setParameter(
            sprintf('filter_idfilter%s',$i) , $filters[$i-1]
    )

instead of this:

    ->setParameters(array(
            'filter_idfilter'.$i => $filters[$i-1],
    ))

Hope this help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.