Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 DQL CONCAT fields and constant strings

I have the fields firstname and lastname in my MySQL table. For convenience, I want to add a computed column to my Doctrine 2 entity called full_name. In plain old MySQL I would do something like this

SELECT CONCAT(firstname, " ", lastname) AS full_name FROM customers;

However, concatenating fields and constant strings (" " in this case) seems not to work with Doctrine's implementation of CONCAT. When using the following code

$repository
    ->createQueryBuilder('customer')
    ->select('CONCAT(customer.firstname, " ", customer.lastname) AS full_name')
    // ...

I get the error

[Syntax Error] line 0, col 91: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got '"'

How can I achieve the same behavior as in MySQL?

like image 732
Subsurf Avatar asked Jan 12 '16 23:01

Subsurf


1 Answers

Apparently, strings in DQL can only be encapsulated by single quotes, not double quotes. A brief search in the documentation did not turn up a direct mention of this behavior, but I noticed that all examples which included constant strings used single quotes.

Changing

->select('CONCAT(customer.firstname, " ", customer.lastname) AS full_name')

to

->select('CONCAT(customer.firstname, \' \', customer.lastname) AS full_name')

or

->select("CONCAT(customer.firstname, ' ', customer.lastname) AS full_name")

solved the issue

like image 127
Subsurf Avatar answered Nov 16 '22 06:11

Subsurf