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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With