phpsymfonydoctrine-ormquery-builder

Doctrine Query Builder andWhere not working


Can someone give me an explanation on why the andWhere part of my query does not work ?

Here is my function:

    public function searchSpectacle(request $request){
    $data = $request->request->all();

    $terms = explode(" ", $data["searchterms"]);
    $date = str_replace('/', '-', $data['datestring']);

    $query = $this->getDoctrine()->getManager()->createQueryBuilder();
    $query->select('S')
            ->from('AppBundle:Spectacle','S');
    $i = 0;
    $parameters = array();

    foreach ($terms as $key => $term){
        $query->orWhere('LOWER(S.nom) LIKE ?' . $i++);
        $parameters[] = '%' . $term . '%';
    }
    foreach ($terms as $key => $term){
        $query->orWhere('LOWER(S.artiste) LIKE ?' . $i++);
        $parameters[] = '%' . $term . '%';
    }

    if(!empty($date)){
        $query->andWhere('S.date = ?');
        $parameters[] = $date;
    }



    $query->setParameters($parameters);
    $spectacles = $query->getQuery()->getResult();

    return $this->render('liste_complete.html.twig', array('spectacles' => $spectacles));
}

Everything works perfectly but when I add the

    if(!empty($date)){
        $query->andWhere('S.date = ?');
        $parameters[] = $date;
    }

part it trows this : Invalid parameter format, ? given, but : or ? expected.

Any ideas ?

Thank you!

Guillaume


Solution

  • probably you miss to concatenate the param id as done in the other parameters. Try this:

    if(!empty($date)){
        $query->andWhere('S.date = ?' . $i++);
        $parameters[] = $date;
    }
    

    Hope this help