4
votes

Doctrine query builder allows me to concat two fields only.

class Expr {
// ...
public function concat($x, $y); // Returns Expr\Func

To concatenate 3 fields I use:

$qb->expr()->concat(
    'table.field1',
    $qb->expr()->concat('table.field2', 'table.field3')
);

And the SQL will be:

CONCAT('table.field1', CONCAT('table.field2', 'table.field3'))

How to get one concat?

When I try to call directly

new Expr\Func('CONCAT', array('table.field1', 'table.field2', 'table.field3'));

Executing query gives me an error

[Syntax Error] line 0, col 237: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

Dumping DQL:

CONCAT('table.field1', 'table.field2', 'table.field3')

Dumping SQL using $qb->getQuery()->getSQL():

[Syntax Error] line 0, col 237: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

1

1 Answers

2
votes

CONCAT with variable number of arguments has been introduced in Doctrine ORM version 2.4.0, so you are probably using an older version. I've tested a similar DQL in my project and works as expected.

You should upgrade your Doctrine ORM deps with:

php composer.phar require doctrine/orm:~2.4

Note that (for now) variable arguments are supported only by Expr\Func constructor, but is not supported in Expr::concat method; you still need this syntax:

new Expr\Func('CONCAT', array('table.field1', 'table.field2', 'table.field3'));

UPDATE I've created a PR on GitHub to support multiple arguments in Expr::concat