1

Doctrine2.5 With PHP 5.6

I have an array of things to search, example: in a table of user, i want to search for the names of everyone that have the name "Reis" or "Shimidt" in the field name. With the array

$arraySearch = ['Reis', 'Shimidt'];

I want to bring for example the following entries,

  • John Reis Carlson,
  • Mary Shimidt Lincoln,
  • Bill Reis Abdonor Gates.

I tried something like this:

$this->query->andWhere(" pb.name LIKE '%:name%' ");
$this->query->setParameter('name', $name, \Doctrine\DBAL\Types\Type::SIMPLE_ARRAY);

It doesnt work, i also tried like this, but obviously returned array to string conversion:

$this->query->setParameter('name', '%'.$name.'%', \Doctrine\DBAL\Types\Type::SIMPLE_ARRAY);

Any ideas how to solution this, without to do a messy code ?

2 Answers 2

1

I advise you to use something like this:

foreach ($arraySearch as $search) {
    $this->query->orWhere(" pb.name LIKE :search ");
    $this->query->setParameter("search", '%'.$search.'%');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help you! Accept the answer to help other persons with your same problem @AlbertAbdonor
0

Try this :

$this->query->setParameter('name', implode("%', OR name LIKE '%", $arraySearch));

1 Comment

Edited, sorry about that

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.