Search code examples
phpdoctrinesymfony4

Query an array type with php array using doctrine


I need to query an mysql array type with php array by using doctrine.

This my db column is dc2 type array and contains values like this.

a:1:{i:0;s:3:"4wd";}

And this is my querybuilder query,

$qb = $this->createQueryBuilder('c'); 
$qb->andWhere('c.equipment IN (:equipment)')
   ->setParameter('equipment', array('4wd'));
$qb->getQuery()->getResult();

But unfortunately this query returns only null output. It would be great if someone can help solve this.


Solution

  • Finally I found an answer with REGEXP, But for symfony 4 you need DoctrineExtensionsBundle

    "beberlei/DoctrineExtensions": "0.3.x-dev",
    

    in doctrine.yaml

    doctrine:
        orm:
            dql:
                string_functions:
                    regexp: DoctrineExtensions\Query\Mysql\Regexp
    

    in repocitery

    $equipment = implode("|", array('4wd', 'AbD'));
    $qb->andWhere('REGEXP(c.equipment, :regexp) = true')
                        ->setParameter('regexp', $equipment);
    

    These two threads were really helpful for the answer and background. MySQL LIKE IN()? And Regex with Doctrine 2 query builder?