Search code examples
phpmysqldoctrinerepositorysymfony-3.3

search in a string with multiple values Symfony 3


I have a table that contains all the postal codes of a country. sometimes the postal code is a single value like (01090) and sometimes it's a postal code multiple (75000-75001-75002 ...). the column type is a string.

        $em = $this->getDoctrine()->getManager();
        $villeCp = $em->getRepository('AppBundle:VillesFranceFree')->findBy(array('villeCodePostal' => $cp)) ;

when I do research on a city whose postal code is equal to 01090 it works and I had the right result but when I try to find the city with the postal code 75000 it returns nothing because in the table this city has a multiple code (75000 or 75001 or 75002) how can I do my search if the code is multiple !


Solution

  • Maybe a query with a like could do so.

    $query = $em->createQuery(
        'SELECT villeCodePostal
        FROM App\Entity\VillesFranceFree v
        WHERE v.villeCodePostal LIKE :cp'
    )->setParameter('cp', '%'.$cp.'%');
    
    $villeCp = $query->execute();