I am trying to create a query using SF2 / Doctrine Querybuilder for a search field.
In my search box, I want to be able to enter several keywords and get results that match all keywords.
So here is my code (in the repository):
foreach ($keywordsArray as $keyword)
{
$qb->andWhere($qb->expr()->orX(
$qb->expr()->like('p.name', ':keyword'),
$qb->expr()->like('p.surname', ':keyword')
));
$qb->setParameter('keyword', '%'.$keyword.'%');
var_dump($keyword);
}
Problem: the 'keyword' parameter is always the last element of $ keywordsArray ...
So, for example, when I type “John Smith” in the search field, I have:
$ keywordsArray
array (size=2)
0 => string 'John' (length=4)
1 => string 'Smith' (length=5)
var_dump ($ keyword) inside the loop tells me that $ keyword = John and then $ keyword = Smith.
BUT , the Symfony profiler tells me that the executed request is complete:
SELECT [...]
FROM [...]
WHERE (l0_.name LIKE '%Smith%' OR a1_.surname LIKE '%Smith%')
AND (l0_.name LIKE '%Smith%' OR a1_.surname LIKE '%Smith%')
I expect:
SELECT [...]
FROM [...]
WHERE (l0_.name LIKE '%John%' OR a1_.surname LIKE '%John%')
AND (l0_.name LIKE '%Smith%' OR a1_.surname LIKE '%Smith%')
Do you have any explanation / solution? What should I change to solve the problem?