Search code examples
symfonydynamicdoctrine-ormdqlsymfony-3.4

Real dynamic DQL with doctrine in symfony 3.4


I'm trying to achieve a dynamic query with DQL in doctrine. I've checked several post about this subject but all the solutions are static. I want to achieve somethin like this:

$qb->where(
            $qb->expr()->orX(
                $qb->expr()->like('e.cliente', ':cliente_tag'),
                $qb->expr()->like('e.cliente', ':cliente_tag2'),
                $qb->expr()->like('e.cliente', ':cliente_tag3')
            ),
            $qb->expr()->orX(
                $qb->expr()->like('e.apoderado', ':apoderado_tag'),
                $qb->expr()->like('e.apoderado', ':apoderado_tag2'),
                $qb->expr()->like('e.apoderado', ':apoderado_tag3')
            )
        );

but inside a loop like this:

foreach ($options['camposTexto'] as $i => $campoTexto) {
             switch ($campoTexto['appliedTo']) {
                 case 'apoderado': {
                     $exp = [];
                     foreach ($campoTexto['tags'] as $tag) {
                         $exp[] = $qb->expr()->like('e.apoderado', ':apoderado_tag' . $i);
                         $parameters['apoderado_tag' . $i] = '%' . $tag . '%';
                     }

                     if ($isFirst) {
                         $isFirst = false;
                         $qb->where($qb->expr()->orX($exp));
                     } else {
                         $qb->andWhere($qb->expr()->orX($exp));
                     }

                     break;
                 }
                 case 'cliente': {
                     $exp = [];
                     foreach ($campoTexto['tags'] as $tag) {
                         $expresiones[] = $qb->expr()->like('e.cliente', ':cliente_tag' . $i);
                         $parameters['cliente_tag' . $i] = '%' . $tag . '%';
                     }

                     if ($isFirst) {
                         $isFirst = false;
                         $qb->where($qb->expr()->orX($exp));
                     } else {
                         $qb->andWhere($qb->expr()->orX($exp));
                     }

                     break;
                 }
             }
         }

tags is an array of strings. As you see I passed the array of expresions but doctrine throws me an Exception.

So far so now I have not found any solution to my problem.

Any Idea?

Thanks in advance!


Solution

  • Looking at this post I figured out the solution. It would be something like this:

        case 'apoderado': {
            $orX = $qb->expr()->orX();
            foreach ($campoTexto['tags'] as $y => $tag) {
            $orX->add($qb->expr()->like('e.apoderado', $qb->expr()->literal('%' . $tag . '%'))); //<= with literal because I can't set the parameters later in the qb
       }
      $expresiones[] = $orX;
      break;
      }
    

    after all the case/break

      $andX = $qb->expr()->andX();      
      $qb->where($andX->addMultiple($expresiones));   
      return $qb->getQuery()->getResult();