Search code examples
phpsymfonydoctrinequery-builderdql

DQL Concat many strings


I want to concat many strings with DQL but I have an error message when I try.

My code:

$qb = $this->_em->createQueryBuilder();
$qb->select('u')
   ->from(Tutore::class, 'u')
   ->andWhere($qb->expr()->concat($qb->expr()->concat('u.nom', $qb->expr()->literal(' ')), 'u.prenom'), ':fullname')
   ->andWhere($qb->expr()->eq('u.ancien', 0))
   ->setParameter('fullname', $fullname);

return $qb->getQuery()->getOneOrNullResult();

The error message :

Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Syntax Error] line 0, col 77: Error: Expected =, <, <=, <>, >, >=, !=, got 'AND'" at /vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 54

I also tried by replacing this :

$qb->expr()->literal(' '))

By a simple : " ".


Solution

  • Very simple:

    ->andWhere("concat(u.nom, ' ', u.prenom) = :fullname")
    .............................................................................