I'm a student and, for my final project, I need to make a searchtool.
I know how to search if a string is contained in my database but I want to search with an array now. So I do it and that's work... but only if it's exactly what it's in my array, not if it's contained.
So my Repository function is like this
public function findByMots($value)
{
return $this->createQueryBuilder('a')
->andWhere('a.titre IN (:val) OR a.contenu IN (:val) OR t.theme IN (:val)')
->setParameter('val', $value)
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id')
->orderBy('a.id', 'DESC')
->getQuery()
->getResult()
;
}
My value is an array with string in, but if one string is "raccoon" he doesn't find my article who has a title like "I love raccoon". Does anybody know please?
I found a method!
public function findByMots($value)
{
$search = $this->createQueryBuilder('a')
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id');
$number = 0;
foreach ($value as $valu) {
$search = $search->orWhere('a.titre LIKE :val' . $number . ' OR a.contenu LIKE :val' . $number . ' OR t.theme LIKE :val' . $number . '')
->setParameter('val'.$number, '%'.$valu.'%');
$number++;
}
$search = $search
->orderBy('a.id', 'DESC')
->getQuery()
->getResult();
return $search;
}