Doctrine 2 Query with LIKE

AlOpal19 picture AlOpal19 · Oct 3, 2012 · Viewed 66k times · Source

I have this code for query:

$repository = $em->getRepository('AcmeCrawlerBundle:Trainings');
       $query = $repository->createQueryBuilder('p')
               ->where('p.title LIKE :word')
               ->orWhere('p.discription LIKE :word')
               ->setParameter('word', $word)
               ->getQuery();
$trainings = $query->getResult();

The problem is: even if matches exist, they not found by this query. I used this code to see full sql:

print_r(array(
        'sql'        => $query->getSQL(),
        'parameters' => $query->getParameters(),
        ));

And what I've got:

FROM Trainings t0_ WHERE t0_.title LIKE ? OR t0_.discription LIKE ? [parameters] => Array ( [word] => Spoken ) 

(last part of query) Tell me please what to change?

Answer

Elnur Abdurrakhimov picture Elnur Abdurrakhimov · Oct 3, 2012

You forgot the % signs around the word:

->setParameter('word', '%'.$word.'%')